T
Thomas Stegen
Frane said:But if I use a char* p="test";
In that case the computer will allocate some space for the array,
right?
Yes, but this is different. The compiler will find memory for the string
literal, then you make the pointer point to it. (Note that the string
cannot be changed).
Memory is also allocated for the string when you do:
puts("test");
It is in fact also not impossible for the string in the call to puts and
the string pointed to by p to be the same string. Remember that the
value you get from evaluating "test" is a pointer. Also in the above you
are assigning to the pointer, not to wherever the pointer is pointing.
In the case of
int *ip;
*ip = 42;
You are dereferencing ip and you get whatever it is pointing to. Bad
thing. Note that 42 evaluated to an int not an address.