According to the text at http://www.cplusplus.com/reference/string/string/, string library in C++ is a class, not just a ” mere sequences of characters in a memory array”.
I wrote this code to find out more:
string s = "abcd";
cout << &s << endl; // This gives an address
cout << s[0] << endl; // This gives 'a'
cout << &s[0] << endl; // This gives "abcd"
I have some questions:
1. Is string library in C++ still an array of sequence characters?
2. How can I get the address of each character in string? (As in the code, I can retrieve each character, but cannot get its address using & operator)
Much (most) of this really isn’t about the string class itself.
std::stringdoes store its contents as a contiguous array of characters.&s[0]will yield the address of the beginning of that array — butstd::ostreamhas an overload ofoperator<<that takes a pointer to char, and prints it as a string.If you want to see the addresses of the individual characters in a string, you need to take their addresses and then cast each address to pointer to void.
std::iostreamalso has an overload ofoperator<<that takes a pointer to void, and that overload prints out the address instead of a string that (it assumes) is at that address.Edit: demo code:
Result: