From Strings in Depth, I learn that the exact implementation of memory layout for the string class is not defined by the C++ Standard. But how come &string
and &string[0]
will have different memory addresses?
string variable("String Test");
cout << (void *) &variable << endl; // 003EFE58
cout << (void *) &variable[0] << endl; // 003EFE5C
cout << (void *) &variable[1] << endl; // 003EFE5D
... ...
But they do share the same content, check out:
cout << ((string)*(&variable))[0] << endl; // 'S'
cout << *(&variable[0]) << endl; // 'S'
Can anyone explain what's going on here?