一个小错误: deprecated conversion from string constant to char*错误的修改

当我们将一个character pointer variable 初始化成一个string literal的时候, 就会出现此类错误。

在最新的C标准或者C++标准中, 使用如下语句, 无论使用gcc 或者g++命令, 都会报出上面的错误, 不能通过编译:

char* x = "hello";
  
  
修改方案如下。

sol1: char* 改为const char* 修饰即可:

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">const char* x = "hello";</span>
  
  

sol2: 方案是使用string:

string x = "hello";
  
  

sol3: 将string literal转型为char* 的type:

char* x = (char*)"hello";



猜你喜欢

转载自blog.csdn.net/qq_26565435/article/details/82783836