Linux下编译代码错误-warning: deprecated conversion from string constant to 'char*

在windows 的VS2013下能编译运行的代码在Linux下用gcc编译不通过是很有可能的,最近编译一段代码,就遇到这种错误,是关于字符串的。
在windows下,下面代码能编译通过

 char** parameter_string(){
    char**  arg = new char*[2];
    arg[0] = "result.txt";  //save line segment
    arg[1] = "result.pgm";  //save result image

    return arg;
    }

但是在Linux下用gcc编译时候,就会遇到标题提示的错误。
原因:为什么呢?原来char *背后的含义是:给我个字符串,我要修改它。
而理论上,我们传给函数的字面常量是没法被修改的。
所以说,比较和理的办法是把参数类型修改为const char *。
这个类型说背后的含义是:给我个字符串,我只要读取它。
所以要加const,但是这个代码里面涉及到了动态分配内存空间,所以怎么加const就成了一个问题。

最开始,我是这样加的

const char** parameter_string(){
    const char**  arg = newconst char*)[2];
    // const char*arg[2];
    arg[0] = "result.txt";  //save line segment
    arg[1] = "result.pgm";  //save result image

    return arg;
    }

这样加const编译会报错:error: array bound forbidden after parenthesized type-id

最后在stack overflow上找到解法:
这里写图片描述
于是就有了下面的代码

const char** parameter_string(){
    const char**  arg = new const char*[2];
    // const char*arg[2];
    arg[0] = "result.txt";  //save line segment
    arg[1] = "result.pgm";  //save result image

    return arg;
    }

就是把new后面的()去掉了;
然后这个错误就过去了。

后面我又遇到了一个新的错误。下篇博客分享。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值