为什么我会收到警告? [英] Why am I getting warning for this?

查看:114
本文介绍了为什么我会收到警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 char * str =My_Name; 





警告:不推荐将字符串常量转换为'char *'[ - 写字符串] |



我尝试过:



我不明白这个警告的原因。

解决方案

原因是你试图初始化一个字符串(char *) - 这不是常数 - 具有恒定值...

您有两种方法可以消除警告:

1.关闭标志 - -Wno-write-string

2.写出正确的代码:

2.1 const char * str =My_Name;

2.2 char str [] =我的名字;

char *str="My_Name";



warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

What I have tried:

I don't understand the reason for this warning.

解决方案

The reason is the way you try to initialize a string (char*) - that is not constant - with a constant value...
You have two ways of getting rid of the warning:
1. Turn the flag off - -Wno-write-string
2. Write the right code:
2.1 const char *str = "My_Name";
2.2 char str[] = "My-Name";


这篇关于为什么我会收到警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆