Initializing an array of char – Is the string used to initialize the array stored separately in memory in addition to where the array is stored

  Kiến thức lập trình

In the book entitled Understanding and Using C Pointers by Richard Reese, on page 110 the author states

“An array of char can be initialized using the initialization operator. In the following example, a header array is initialized to the character contained in a string literal:

char header[] = "Media Player";

At a later point it states:

“…The initialization will copy these characters to the array terminated by the NUL character, as illustrated in Figure 5-2, assuming the declaration is located in the main function.

In Figure 5-2 (Initializing an array of char) on page 111 it shows the header array located on the stack with its memory address but it also shows the string that was used to initialize the array located in the String Literal Pool at a different memory address.

I find this at odds with what I have read in the book by K.N. King C Programming A Modern Approach 2nd Ed. where on on page 281 an array is initialized as follows

char date1[8] = "June 14";

Later on page 282 it states:

Although “June 14” appears to be a string literal, it’s not. Instead, C views it as an abbreviation for an array initializer.

Based on the latter text, and my limited understanding, I am thinking that the text Understanding and Using C Pointers is incorrect in stating that “Media Player” is a string literal when used to initialize the array — it doesn’t appear to be simply using the term ‘string literal’ loosely as a synonym for string as the text then goes on to incorrectly(?) show that “Media Player” is located in the String Literal Pool with its own memory address.

Is my understanding correct?

New contributor

Rad Stor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT