Search code examples
cstringpointersmemorystring-literals

What happens if I try to define a pointer which points to a string literal in C?


For example,

char * first = "Hello World!";

My questions is,

  1. Is the string literal stored in memory? (If it is I guess the pointer first is the address of inital element of the string literal "Hello World")

  2. If not, then is some kind of random pointer value stored in first?


Solution

  • Yes, string literals are stored in memory. The C and C++ standards just say that string literals have static storage duration, any attempt at modifying them gives undefined behavior, and multiple string literals with the same contents may or may not share the same storage.