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版本比较高时,编译代码可能出现的问题

问题是这样产生的,先看这个函数原型:

1
void someFunc(char *someStr);

再看这个函数调用:

1
someFunc("I'm a string!");

把这两个东西组合起来,用最新的g++编译一下就会得到标题中的警告。

为什么呢?原来char *背后的含义是:给我个字符串,我要修改它。

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

所以说,比较和理的办法是把参数类型修改为const char *。

这个类型说背后的含义是:给我个字符串,我只要读取它。

如何同时接收const类型和非const类型?重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
 
int fuc(char *a)
{
    cout << a << endl;
}
int fuc(const char *a)
{
    cout << a << endl;
}
int main()
{
    char a[] = "hello 123";
    fuc(a);
    const char b[] = "hello 123";
    fuc(b);
}

deprecated conversion from string constant to ‘char*’的更多相关文章

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

    warning: deprecated conversion from string constant to 'char* #include<iostream> using namespa ...

  2. warning:deprecated conversion from string constant to 'char *' 解决方案

    #include <iostream> using namespace std; int fuc(char *a) { cout << a << endl; } i ...

  3. warning:deprecated conversion from string constant to &#39;char *&#39;

    warning:deprecated conversion from string constant to 'char *' 解决方式 #include <iostream> using ...

  4. warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    在C++中, char* p = "abc"; // valid in C, invalid in C++ 会跳出警告:warning: ISO C++ forbids conve ...

  5. warning: ISO C++ forbids converting a string constant to 'char*'

    第1种字符串赋值方式: char * fileName="./2017-09-02-10-34-10.xml";//这一种字符串赋值方式已经被ISO禁止了 第2种字符串赋值方式: ...

  6. ISO c++11 does not allow conversion from string literal to 'char*'

    http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char

  7. 将string转换成char*

    string 是c++标准库里面其中一个,封装了对字符串的操作  把string转换为char* 有3中方法:  1.data  如:  string str="abc";  ch ...

  8. Constant Pool和String Constant Pool详解

    Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太明白Constant Pool到底是个怎 ...

  9. 将string转换成char* (转)

    原文:http://blog.sina.com.cn/s/blog_786ce14d01014lpr.html string 是c++标准库里面其中一个,封装了对字符串的操作把string转换为cha ...

随机推荐

  1. AppICon设置

  2. python 基础 6.2 raise 关键字使用

    一. raise 关键字    raise 用来触发异常    语法如下:     raise[Exception [,args [,traceback]]]     语句中Exception 是异常 ...

  3. python 基础 2.2 if流程控制(二)

    一. if  else   1.逻辑值(bool)包含了两个值: ----True:表示非空的值,比如:string ,tuple,list,set,dictonary,所有非空的序列. -----F ...

  4. C#Panel 控件的使用

    Windows 窗体 Panel 控件用于为其他控件提供可识别的分组.通常,使用面板按功能细分窗体.例如,可能有一个订单窗体,它指定邮寄选项(如使用哪一类通营承运商).将所有选项分组在一个面板中可向用 ...

  5. vs2013工程技巧

    1 vs工程输出了dll和lib,分别是什么,有什么用? 当设置工程property的Project Defaults的Configuration Type为dll时,不光会生成该动态链接库的dll文 ...

  6. CUDA: 常量内存与事件

    常量内存: 常量内存用于保存在核函数执行期间不会发生变化的数据,在变量面前添加  __constant__  修饰符: __constant__  Sphere  s[SPHERES]; cudaMe ...

  7. 单机部署tomcat的shell脚本

    单机部署tomcat的shell脚本,来自网络,自己需要时要根据自己的需求改动. #!/bin/sh # ############################################### ...

  8. linux下 python源码包解压报错

    执行下面的命令 tar -zvxf Python.3.6.5.tgz 报错 gzip: stdin: not in gzip format tar: Child returned status 1 t ...

  9. OC中RAC编程block的基本使用

    在OC中block的基本使用 // // ViewController.h // RAC--test // // Created by Aaron on 17/1/17. // Copyright © ...

  10. 重新实践c++primer上面的代码

    又重新敲了敲c++primer上面的代码,觉得很有意思,讲的很细,c++真牛逼啊 #include <iostream> #include <string> #include ...