… something like
char c[5] = {'A','B','C','D', 0};
is equivalent to
char* c = "ABCD";
No. The first defines c
to be an array of 5 char
which will be initialized with the values shown. If this declaration appears outside a function, c
will have static storage duration. It will be initialized once when the program begins and will exist for the entire execution of the program. In common C implementations, it will be stored in an initialized-data section. If this declaration appears inside a function, c
will have automatic storage duration. It will be initialized each time execution reaches the declaration. How the initial values are stored is up to the C implementation—they might be built into instructions that initialize the array, or they might be stored in a constant-data section so the program can copy them from there to the new instance of c
whenever one is created.
The second defines c
to be a pointer to a char
. The string literal nominally defines an array of static storage duration. In common C implementations, that array, if it is actually needed (optimization could make it unnecessary) will be stored in a constant-data section. c
is initialized to point to the first character of that array. If this declaration appears outside a function, c
has static storage duration, so it is initialized once when the program starts. If it appears inside a function, c
has automatic storage duration, and it is created and initialized each time execution reaches the declaration. In either case, it is initialized to point to the first character of the array defined by the string literal.
EDIT: Follow up question:
char c[5] = {'A','B','C','D', 0};
if I now say c+1, this is a pointe to the character 'B'? But is 'B' in > the stack? or in the code section of memory?
c+1
points to c[1]
. If c
is defined outside of any function, it is, in common C implementations, in some section the compiler/program uses for static data, so c+1
points into that section. If c
is defined inside a function, it is, in common C implementations, on the stack, so c+1
points into the stack. (Note that optimization may make it unnecessary to store all of c
or to maintain c+1
as a pointer at all, depending on the context.)
But I always get confused as to what it means when we say that STRING > LITERALS ARE STORED IN THE CODE SECTION OF MEMORY.
A string literal defines an array of static storage duration. In common C implementations, they will be stored in a constant-data section. This different from a code section. They are both read-only, but a code section is executable. Some computer architectures do not have means of distinguishing them, and it is certainly possible to store read-only data in a code section, but it is generally better for there to be separate sections.
As with all things in C, the nominal meaning of source code is just how it must behave in an abstract machine the C standard defines. Compilers are allowed to optimize as long as the resulting program has the same behavior in terms of observable effects, which include visible input and output. Optimization might result in a string that has static storage duration in the abstract machine being very different in the actual program.