简体   繁体   中英

How does conversion of string to char pointer work in C++

I need some help understanding how to convert string to a char *pointer .

string text;

char *str = &text[0];

Could someone please kindly explain to me how does the conversion of text to char in the above case work?

If text is a string then text[0] is not just a char but a char& to the specified char (a reference to the char), so &text[0] is a char*

But warning, the internal char* of a string may not be null terminated, this is why c_str() exists and forces the null terminating char to be present, and also returns a const pointer because of course this is more secure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM