Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transfer string to char* (not const char*)

Tags:

c++

I wanna do something like:

string result;
char* a[100];
a[0]=result;

it seems that result.c_str() has to be const char*. Is there any way to do this?

like image 331
user1933667 Avatar asked Nov 20 '25 17:11

user1933667


1 Answers

You can take the address of the first character in the string.

a[0] = &result[0];

This is guaranteed to work in C++11. (The internal string representation must be contiguous and null-terminated like a C-style string)

In C++03 these guarantees do not exist, but all common implementations will work.

like image 128
Drew Dormann Avatar answered Nov 23 '25 06:11

Drew Dormann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!