编程语言
首页 > 编程语言> > C++中char[],char*,string 相互转换

C++中char[],char*,string 相互转换

作者:互联网

char[]转char*

char charArray[]="Hello World";
char* charList=charArray;

char[]转string

char charList[]="Hello World";
string str=charList;

char*转string

char* charList="Hello World";
string str=charList;

string转char[]

string str="Hello World";
char charArray[str.length()+1];
strcpy(charArray, str.c_str());

string转char*

string str="Hello World";
char* charList=const_cast<char *>(str.c_str());;

注意

使用 char[] 时一定要注意长度+1,因为他有一个 '\0' 字符,但不显示表达出来

标签:string,C++,char,charList,str,World,Hello
来源: https://www.cnblogs.com/wasi-991017/p/12813949.html