C++——string转char[]

包含头文件

#include <cstring>

 

int main(){

    string e = "abcdef1";
    char buf[] = {0};
    strcpy(buf,e.c_str());
    //cout << e.c_str() << endl;  //c_str是一个内容为字符串指向字符数组的临时指针,好像7行是会出错,不能随便对这个指针进行操作,要通过strcpy函数
    cout << buf[2] << endl;

    char buf1[] = "abcd";
    cout << buf1[2];

    system("pause");
    return 0;
}

 

posted @ 2020-11-21 11:44  倔强的不死人  阅读(398)  评论(0编辑  收藏  举报