码迷,mamicode.com
首页 > 其他好文 > 详细

warning:deprecated conversion from string constant to 'char *'

时间:2015-06-07 01:05:11      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:解决方案   char   constant   

warning:deprecated conversion from string constant to ‘char *’ 解决方案

#include <iostream>
using namespace std;

int fuc(char *a)
{
    cout << a << endl;
}
int main()
{
    fuc("hello");
}

Linux 环境下当GCC版本比较高时,编译代码可能出现的问题。

主要原因是:

char * 指针指向的字符串是允许修改的,将其作为形参,意味着在函数的某处代码可能修改其指向字符串。

而理论上,我们传给函数的字面常量是没法被修改的。

解决方法1.

把函数形参 参数类型修改为const char *。

解决方法2.

如果肯定函数中不会对形参指向变量进行改变。可以用(char *)进行强制转换。

warning:deprecated conversion from string constant to 'char *'

标签:解决方案   char   constant   

原文地址:http://blog.csdn.net/u012675539/article/details/46393453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!