什么是C/C++中C_str()函数的用法

sac*_*hin 3 c++ stdstring c-str

谁能告诉我C/C++中c_str()函数的用途是什么?在哪种情况下有必要使用它?

And*_*rsK 6

当您想要将字符串与C函数一起使用时

string s = "hello";
printf( "your string:%s", s.c_str() );
Run Code Online (Sandbox Code Playgroud)


Bas*_*tch 5

这是 C++ 的东西,而不是 C 的东西。

c_str(来自)的常见用途std::string是将 C++ 转换std::stringconst char*C 字符串,这是许多低级 C 函数(例如 Posix 系统调用stat等)所需要的。