C++ string character array conversion

void main()
{
    //字符串转字符数组
    string name = "ddddd";
    char buf[] = {0};
    strcpy(buf , name.c_str());//字符串转字符数组,使用strcpy
    cout << name.c_str() << endl;//name.c_str()将字符串转换成字符数组
    cout << buf << endl;

    //字符数组转字符串
    char buf1[]= "xxxxxx";
    string name1;
    name1 = buf1;
    cout << name1 << endl;

}

Guess you like

Origin blog.csdn.net/m0_47540684/article/details/128534758