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?
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