如何将c++的string转换为C语言的字符型数组?

2025-04-29 09:43:54
推荐回答(2个)
回答1:

const char* string::c_str();

string a = "test";
puts(a.c_str());

可以得到const char类型的指针,你为什么转换成数组?当然你一定要转换可以将其复制进数组
char sz[5];
string a = "test";
strcpy(sz, a.c_str());

回答2:

oh,my god、
string是个类,它里面有个函数c_str()就是获得string的 c 字符串表示(其实返回的就是个字符指针)

string s = "hello,world.\n";
printf("%s", s.c_str()); //

应该ok了吧、、