const char *p = "abcde"; >The second one explicitly tells the compiler that content should be protected. Wrong. This is how it is. Consider first one : char *p = "abcde".."/>
Posted by: gidilat August 4, 2005
Information technology
Login in to Rate this Post:     0       ?        
>char *p = "abcde"; >const char *p = "abcde"; >The second one explicitly tells the compiler that content should be protected. Wrong. This is how it is. Consider first one : char *p = "abcde"; "abcde" is a string literal. It is a immutable and so should be placed in a memory that can not be modified. char *p tells that p is a pointer to a character. The '=' says that currently p is pointing to the address where "abcde" string resides in the memory. You can later change p to point to any char (whether const or non). Now the second one: const char *p = "abcde"; Same argument for "abcde" const char *p tells p that p is a pointer to const char. You can only use p to point to const chars. Compilers should always protect the string literals. "const char * " - does not tell compiler that the 'content' should be be protected. It only says that the pointer will point to non modifiable locations. The nature of string literal is such that they are placed in non modifiable locations. You could, for example, write; const char *p; char * q = "abcde"; p = (const char *)q; Do not ask me to define hackers.
Read Full Discussion Thread for this article