As I understand it, the following code works like so:
char* cptr = "Hello World";
"Hello World" lives in the .rodata
section of the program's memory. The string literal "Hello World"
returns a pointer to the base address of the string, or the address of the first element in the so-called "array", since the chars are laid out sequentially in memory it would be the 'H'. This is my little diagram as I visualize the string literal getting stored in the memory:
0x4 : 'H'
0x5 : 'e'
0x6 : 'l'
0x6 : 'l'
0x7 : 'o'
0x8 : ' '
0x9 : 'W'
0xa : 'o'
0xb : 'r'
0xc : 'l'
0xd : 'd'
0xe : '\0'
So the declaration above becomes:
char* cptr = 0x4;
Now cptr points to the string literal. I'm just making up the addresses.
0xa1 : 0x4
Now how does this code work?
char cString[] = "Hello World";
I am assuming that as in the previous situation "Hello World"
also degrades to the address of 'H' and 0x4.
char cString[] = 0x4;
I am reading the =
as an overloaded assignment operator when it used with initialization of a char array. As I understand, at initialization of C-string only, it copies char-by-char starting at the given base address into the C-string until it hits a '\0' as the last char copied. It also allocates enough memory for all the chars. Because overloaded operators are really just functions, I assume that it's internal implementation is similar to strcpy()
.
I would like one of the more experienced C programmers to confirm my assumptions of how this code works. This is my visualization of the C-string after the chars from the string literal get copied into it:
0xb4 : 'H'
0xb5 : 'e'
0xb6 : 'l'
0xb6 : 'l'
0xb7 : 'o'
0xb8 : ' '
0xb9 : 'W'
0xba : 'o'
0xbb : 'r'
0xbc : 'l'
0xbd : 'd'
0xbe : '\0'
Once again, the addresses are arbitrary, the point is that the C-string in the stack is distinct from the string literal in the .rodata
section in memory.
What am I trying to do? I am trying to use a char pointer to temporarily hold the base address of the string literal, and use that same char pointer (base address of string literal) to initialize the C-string.
char* cptr = "Hello World";
char cString[] = cptr;
I assume that "Hello World"
evaluates to its base address, 0x4
. So this code ought to look like this:
char* cptr = 0x4;
char cString[] = 0x4;
I assume that it should be no different from char cString[] = "Hello World";
since "Hello World" evaluates to its base address, and that is what is stored in the char pointer!
However, gcc gives me an error:
error: invalid initializer
char cString[] = cptr;
^
- How come you can't use a char pointer as a tempoorary placeholder to store the base address of a string literal?
- How does this code work? Are my assumptions correct?
- Does using a string literal in the code return the base address to the "array" where the chars are stored in the memory?
char cString[] = "Hello World"
does not involve any pointers -- the symbolcString
ends up being the address of an array of 12 characters, initialized with the characters from "Hello World". – Ac