C++ 中的C_str()函数用法

语法: 
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
看看下面的例子,各位就会明白了

int main()
{
string path;
path="ansf";
cout<<path<<endl;;
printf("%s",path.c_str());
return 0;
}

两次都是输出 anaf

 
原文地址:https://www.cnblogs.com/tsw123/p/4366750.html