美文网首页
c++字符串和字符数组互相转

c++字符串和字符数组互相转

作者: 送分童子笑嘻嘻 | 来源:发表于2019-12-19 11:21 被阅读0次
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;

}

相关文章

网友评论

      本文标题:c++字符串和字符数组互相转

      本文链接:https://www.haomeiwen.com/subject/zrrmnctx.html