Hi
Why is that
int *p = 12;
core dumps
This is not valid C, you cannot implicitly convert an integer to a
pointer, except for the special case of 0 which is a null pointer
constant. Any C compiler should complain about the line above.
& not
char *p = "some_str";
If you had
int *p = (int *)12;
you would be convertinbg the value 12 to a pointer type. C doesn't define
what the result of this will be, it certainly doesn't guarantee that it is
a valid pointer.
In the second case you have a string literal. String literals define (i.e.
create) an object of type array of char. In other words they create
something legitimate for a pointer to point at. Other constants such as 12
or 'X' are simply values are don't create their own objects.
Shouldnt the int also be written in the data segment??
C doesn't have a real concept of "data segment". Since 12, and indeed
(int *)12 are just values they don't exist in objects so storage
type/location/method is not really applicable. A reasonable compiler might
well encode values in the instruction stream where it can. However as far
as C is concerned the only important things are:
1. is the code valid,
2. if so what behaviour is required of it.
The question of how such behaviour is brought about is not important and
could well vary from one compiler to the next.
Lawrence