Wednesday, September 1, 2010

Pointer to an array

When we say
string *p = "hello";

we actually doing are allocating hello\0 somewhere in memory we don't know and p points to that memory location.
"hello\0" is a string literal i.e. it is string constant so can't be edited. But in older c days, it was possible that you say
char *p =  "hello";
p = "world";
and get away with it....but now it is not possible as concept of "const" has evolved.

But compilers still allow it for compatibility reasons, so that is still possible. To get stricter compiler, you can add -Wall -pedantic option to g++ and in practice one should do that.

No comments:

Post a Comment