ISO C++ forbids converting a string constant to 'char*'解决方法

forbids converting a string constant to

C++中将字符串常量定义为指针数组中的元素时,C++的编译器抛出warning:并提示:ISO C++ forbids converting a string constant to 'char*',比如下方的这个实例代码。首先,这是什么意思呢?翻译过来就是:ISO C++禁止将字符串常量转换为“char*”(指针)。那该怎么办呢?

#include <iostream>
using namespace std;


int main(){
    char *pstr[2] = {"abc","def"};
    printf("%x\n",pstr[0]);
    return 0;
}

解决方法

使用const关键字,将指针数组转换为常量,比如:const char *pstr[2] = {"abc","def"};

免责声明:内容仅供参考,不保证正确性!


全栈后端 / C++教程 :
















Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.