3

std::string.c_str() returns a (const char *) value. I Googled and found that I can do the following:

std::string myString = "Hello World";
char *buf = &myString[0];

How is this possible? &myString[0] is an object of type std::string, so how can this work?

0

7 Answers 7

9

&myString[0] is a object of type std::string

No it isn't. myString[0] is a reference to the first character of the string; &myString[0] is a pointer to that character. The operator precedence is such that it means &(myString[0]) and not (&mystring)[0].

Beware that, accessed this way, there's no guarantee that the string will be zero-terminated; so if you use this in a C-style function that expects a zero-terminated string, then you'll be relying on undefined behaviour.

Sign up to request clarification or add additional context in comments.

7 Comments

"no guarantee that the string will be zero-terminated" true in C++03, but C++11 does have a guarantee.
@TonyD: No, C++11 doesn't guarantee that the contiguously stored characters will have a zero after them; just that str[size] will give a reference to a zero value, and that the result of c_str() will be terminated.
@MikeSeymour: I wonder why the spec has made it so complicated. Just to save ONE byte?
See 21.4.7.1: const charT* c_str() const noexcept; const charT* data() const noexcept; Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()]. - so &p[i] references within a NUL terminated string (as required by c_str()). (You might want to read stackoverflow.com/questions/6077189/… )
@TonyD: Indeed, c_str() is required to return a pointer to a contiguous, terminated array (as I said). But operator[] is only required to give a reference into a contiguous array for indexes less than size(); see 21.4.5/2 amd 21.4.1/5. A suitably perverse implementation could remain unterminated until someone calls c_str().
|
4

There are const and non-const overloads of std::string::operator[]( size_type pos ), and the non-const version returns a char&, so you can do things like

std::string s("Hello");
s[0] = 'Y';

Note that, since s[0] returns char&, then &s[0] is the address of element s[0]. You can assign that address to a char*. It is up to you not to misuse this pointer.

Comments

3

It has to do with operator precedence. The [] operator has higher precedence than the address-of operator &, so the address-of operator works on the character reference returned by the strings operator[] function.

1 Comment

+1 for talking about precedence. That is a different angle to look at the syntax.
2

The operator [] (std::string::char& operator[] (size_t pos)) overloaded returns a reference to the character at the index. You are taking the address of such character reference which is fine.

So, myString[0] return type is not std::string but char&.

There is no reason to do it. You can directly do -

myString[0] = 'h';

Comments

2

The std::string methods c_str() and operator[] are two diferent methods, which return two different types.

The method c_str() does indeed return a const char*.

const char* c_str() const;

However, the method operator[] returns instead a reference to a char. When you take the address of it, you get the address of a char.

       char& operator[] (size_t pos);
 const char& operator[] (size_t pos) const;

Comments

1

You are wrong. &myString[0] is not of type std::string, it is of type char *.

The [] operator has higher precedence and operator[] returns a reference to char, and its address (the & operator) is of type char *.

Comments

1

The std::string type has an operator[] that allows indexing each one of the characters in the string. The expression myString[0] is a (modifiable) reference to the first character of the string. If you take the address of that you will get a pointer to the first character in the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.