deprecated conversion from string constant to ‘char*’
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*’相关推荐
- warning:deprecated conversion from string constant to 'char *' 解决方案
#include <iostream> using namespace std; int fuc(char *a) { cout << a << endl; ...
- C++: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
不允许将字符串常量,给char指针. warning.c: In function 'int main()': warning.c:9:19: warning: ISO C++ forbids con ...
- linux c++ 警告 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 ...
- warning: ISO C++ forbids converting a string constant to ‘char*‘ [-Wwrite-strings]
warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] 在C++11中有明确规定 char ...
- arm linux c++编译警告:ISO C++ forbids converting a string constant to ‘char*’(需要把const char*转换成char*)
编译时遇到警告: /home/yg/rv/sdk/rv1.8/rv1126_rv1109/buildroot/output/rockchip_rv1126_rv1109/build/ky_ai_cam ...
- C++11编译问题:warning: ISO C++11 does not allow conversion from string literal to ‘char *‘
在新写的采用C++11标准的编译器里,进行编译以后,原来没有任何提示的语句,竟然出现如下问题: char* a="abc"; std::cout << a <&l ...
- linux c++ 警告: ISO C++ forbids converting a string constant to char*
在C++11中有明确规定 char* p = "abc"; // valid in C, invalid in C++ 如果你进行了这样的赋值,那么编译器就会跳出诸如标题的警告.但 ...
- 「 C/C++疑难杂症 」warning: ISO C++ forbids converting a string constant to ‘CHAR*’ {aka ‘char*’} [-Wwrite
文章目录
- 如何将std :: string转换为const char *或char *?
如何将<code>std::string转换为char*或const char* ? #1楼 看看这个: string str1("stackoverflow"); c ...
最新文章
- Python读取多个excel文件(删除字段、数据格式转换、dataframe多表合并)并写入ElasticSearch实战(自动创建索引、写入ElasticSearch、探索性数据分析)
- Ubuntu16.04中WPS不能输入中文
- Cisco easy *** basic ASA
- 单车家族 结对项目二
- 【MySQL】redo log --- 刷入磁盘过程
- Synchronized的使用详解与区别(干货满满!!!)
- js设置html不可复制,用JS实现用户禁止WEB复制
- 【IoT】基于NB-IoT的CoAP协议浅析
- Python生成二维码-qrcode
- 使用fastcgi_cache加速Nginx
- excel学习-阶梯查询函数lookup
- linux mint五笔输入法,Linux Mint安装ibus五笔和拼音输入法简明教程
- 文本数据挖掘----初识数据挖掘
- kettle整合kafka
- php对视频分割,如何把一个视频无损分割成多个视频片段(最好是质量和格式无变化)...
- CentOS 7下atime如何变化的问题(转)
- idea 中部署 tomcat
- minimax算法及α-β剪枝算法
- 用jQuery提取到input中value值 checked选择器
- 美国医生推荐感冒食疗方