c++11 does not allow conversion
时间: 2024-06-02 21:07:42 浏览: 7
Sorry, can you please provide more context or information about your question? I'm not sure what you are asking or what specific problem you are encountering. Is it related to programming or software development?
相关问题
ISO C++11 does not allow conversion from string literal to 'char *'
In ISO C++11, string literals are considered to have type `const char[]`, which means they cannot be converted to a non-const `char*` directly. This change was made to enforce stricter type safety and prevent accidental modification of string literals.
If you need to pass a string literal to a function that expects a `char*` argument, you can use a const-correct approach by declaring the parameter as `const char*`. For example:
```cpp
void myFunction(const char* str) {
// Function implementation
}
int main() {
myFunction("Hello, world!"); // Passing string literal as const char*
return 0;
}
```
Alternatively, if you have control over the function signature, you can update it to accept a `const char*` parameter instead of `char*` if the function doesn't need to modify the string.
workspace/2.c/main.cpp:6:16: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings] char *p ="1234567890"; ^ 1 warning generated.
这段代码在编译的时候给出了一个警告,警告内容为ISO C++11不允许将字符串字面值转换为'char *'。具体警告信息为:在第6行,将字符串字面值"1234567890"赋值给了一个char类型的指针p,而ISO C++11不允许这样做,因为字符串字面值是常量,不能被修改,而指针p又是可写的,可能会导致程序运行出错。建议将指针p的类型改为const char*,表示指向常量的指针,可以避免这个警告。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![war](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)