c++中string类成员函数c_str()

来源:互联网 发布:出租屋网络方案 编辑:程序博客网 时间:2024/05/17 22:01

1.string类成员函数c_str()的原型:

                  const char *c_str()const;//返回一个以null终止的c字符串

 

2.c_str()函数返回一个指向正规c字符串的指针,内容和string类的本身对象是一样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式;

 

3.操作c_str()函数的返回值时,只能使用c字符串的操作函数,如:strcpy()等函数.因为,string对象可能在使用后被析构函数释放掉,那么你所指向的内容就具有不确定性.

 

eg:

char  * name[20];

string  ptr = "tongnono";

strcpy(name,ptr.c_str());//c_str()返回的是一个临时的指针变量,不能对

0 0
原创粉丝点击