what if i have something like this
char *str="Hello World";
is this imply the str is the pointer to constant data.
as when i do some strcpy kind of operation on str , it will assert.
so can we say that
char *str="Hello World" equal to const char *str="Hello World"
if yes , why is it implicit.
re: what about c style pointer to string
Posted by Smasher/Devourer
on 06/28/2005 09:47pm
Please see this Item from Guru of the Week for a bit of an explanation:
Basically, a C++ program has an area of memory called const data, which stores data whose values are known at compile time, such as string literals. Because a declaration such as the one in your comment creates a pointer aimed directly at this region of memory, rather than allocating new (modifiable) memory and copying the string literal into it, the results of attempting to write to it are undefined.
The declarations of myPtr are switched in the two examples:
The statements are switched in the 1st and second examples:
const char * myPtr
declares a pointer to a constant character. You cannot use this pointer to change the value being pointed to:
char char_A = 'A';
--> char * const myPtr = &char_A; // should be const char* myPtr ...
*myPtr = 'J'; // error - can't change value of *myPtr
Posted by Brad Jones
on 04/16/2004 05:32pm
That is what I get for letting someone get me to post it before I proofed it. It should be a bit better now (assuming the cache on the site has refreshed the article ).