Trying to wrap my head around Chapters 5.7 to 5.9 in "The C Programming Language", handling with multi-dimensional arrays, arrays of pointers etc., I came up with the following observation:
If in the code below foo is declared as a pointer to a char array, and I later want to assign a pointer of type char to it, I must precede the string literal with a & symbol. However, if I declare bar as a pointer, the very same operation is possible without a & symbol.
char (*foo)[3]; // creates a single pointer to a char array of size 3
char *bar; // creates a single pointer to char
int main()
{
foo = &"AB";
bar = "AB";
return 0;
}
The disassembly (64bit Macho-O) seems (at least to the beginner's eye) to perform the same operations for both assignments:
Disassembly of section __TEXT,__text:
0000000100003f80 _main:
100003f80: 55 push rbp
100003f81: 48 89 e5 mov rbp, rsp
100003f84: 31 c0 xor eax, eax
100003f86: 48 8d 0d 7b 00 00 00 lea rcx, [rip + 123] // address of 'bar'
100003f8d: 48 8d 15 6c 00 00 00 lea rdx, [rip + 108] // address of 'foo'
100003f94: c7 45 fc 00 00 00 00 mov dword ptr [rbp - 4], 0
100003f9b: 48 8d 35 08 00 00 00 lea rsi, [rip + 8] // address pointing to 'AB'-String
100003fa2: 48 89 32 mov qword ptr [rdx], rsi // store address of 'AB' in 'foo'
100003fa5: 48 89 31 mov qword ptr [rcx], rsi // store address of 'AB' in 'bar'
100003fa8: 5d pop rbp
100003fa9: c3 ret
Disassembly of section __TEXT,__cstring:
0000000100003faa __cstring:
100003faa: 41 42 <unknown>
100003fac: 00 <unknown>
Disassembly of section __DATA,__common:
0000000100004000 _foo:
...
0000000100004008 _bar:
...
Since the book I am reading is my first contact to C, I'm afraid that I'm missing something obvious here. Wouldn't it be more logical, if I would need the &-symbol in both cases?