Excellent Job Boss............. Very Informative... Keep it UpReply
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.
Please see this Item from Guru of the Week for a bit of an explanation: http://www.gotw.ca/gotw/009.htm 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.
ReplyThe 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
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