Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串转换为常量字符*问题_C++_String - Fatal编程技术网

C++ 将字符串转换为常量字符*问题

C++ 将字符串转换为常量字符*问题,c++,string,C++,String,我犯了一个错误 无法在初始化中将'std::string{aka std::basic_string}'转换为'const char*' 如何将字符串强制转换为常量字符* 谢谢你的帮助 如何将字符串强制转换为常量字符* 使用函数,它将返回字符串的标准C字符数组版本 string str1 = "hello"; const char* string1 = str1; 尝试const char*string1=str1.c_str()这个解决方案怎么样: const char* stri

我犯了一个错误

无法在初始化中将'std::string{aka std::basic_string}'转换为'const char*'

如何将字符串强制转换为常量字符*

谢谢你的帮助

如何将字符串强制转换为常量字符*

使用函数,它将返回字符串的标准C字符数组版本

string str1 = "hello";
    const char* string1 = str1; 

尝试
const char*string1=str1.c_str()

这个解决方案怎么样:

const char* string1 = str1.c_str();

在下一次调用
字符串的非常量成员(或字符串对象的析构函数)之前,它是有效的。str1.data()不能保证以null结尾,所以我会对此小心一点。没错。谢谢
string str1 = "hello";
const char* string1 = str1.c_str();