gcc_warning:assignment makes integer from pointer without a cast
来源:互联网 发布:淘宝优惠券活动报名 编辑:程序博客网 时间:2024/05/18 03:37
在使用gcc对源文件进行编译的时候,往往会出现各种各样的警告或者错误。如果知道这些警告或者错误的意思,就方便我们去对程序进行修改。然而,由于这些警告和错误都是英文了,而且有很多是晦涩难懂的,在此有必要做一下总结。
warning: assignment makes integer from pointer without a cast [enabled by default]
百度翻译:分配使得整数指针没有投。
有道翻译:作业使整数指针不投。
以上两个词典的翻译结果都是狗屁不通的,根本没有办法理解。那么,唯有从源代码中分析问题,然后根据问题去理解这句话的意思了。
代码段1:
char *c="hello world";
*c = "hello";//赋值一个字符串
编译代码段1时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]
执行代码段1时,提示:Segmentation fault (core dumped)
代码段2:
char *c="hello world";
*c = 123;//赋值一个整数
编译代码段2时,提示:没有任何提示
执行代码段2时,提示:Segmentation fault (core dumped)
代码段3:
char *c=""hello world";
*c = 't';//赋值一个字符
编译代码段3时,提示:没有任何提示
执行代码段3时,提示:Segmentation fault (core dumped)
代码段4:
char *c;
*c = 123;//赋值一个整数
编译代码段4时,提示:没有任何提示
执行代码段4时,提示:Segmentation fault (core dumped)
代码段5:
char *c;
*c = 't';//赋值一个字符
编译代码段5时,提示:没有任何提示
执行代码段5时,提示:Segmentation fault (core dumped)
代码段6:
char *c;
*c = "t";//赋值一个字符串
编译代码段6时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]
执行代码段6时,提示:Segmentation fault (core dumped)
分析对比上述的6个代码段可知,不管char *c有无被初始化(指向确定的地址),*c接受int型,字符型的输入时,都不会出现warning(char 和int都可以理解为整型);但是,只要*c接受字符串型的输入,就会提示warning!
由于字符窜在c中通常是以其首地址来表示,因此将“hello world”赋值给*c,其实是将其首地址赋值给*c。而*c只接受整型的输入,因此默认情况下,将这个地址(char *型)转换为整型(int 型)。
根据上面的提示,可以再设计一段代码,如下面代码7所示。
代码段7:
char c;
c = “hello”;//赋值一个字符串
编译代码段7时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]
执行代码段7时,提示:没有任何提示。
由代码段7可知,其实这个warning根本就是由于变量的类型(char)和赋值的类型(字符串)不匹配所导致的!类型不匹配时,自然就采用默认的类型转换方式[enabled by default]。
代码段8:
char c;
c = (char)“hello”;//赋值一个字符串
编译代码段8时,提示: warning:cast from pointer to integer of different size [-Wpointer-to-int-cast]
执行代码段8时,提示:没有任何提示。
代码段9:
char *c=0;
编译代码段9时,提示:warning: initialization makes pointer from integer without a cast [enabled by default]
执行代码段9时,提示:没有任何提示。
代码段10:
char *c=(char *)0;
编译代码段10时,提示:没有警告!
执行代码段10时,提示:没有任何提示。
结论:根据以上分析,可以知道warning的意思是:赋值由指针制造了一个整数,并且没有强制类型转换[使用默认类型转换]。cast在这里表示强制类型转换!
拓展:代码段1~6运行都会出现错误,说明对没有初始化的指针所指向的地址赋值是不允许的;如果一个指针初始化为为字符串,同样不可以再对它进行修改!
- gcc_warning:assignment makes integer from pointer without a cast
- warning: assignment makes pointer from integer without a cast错误
- assignment makes pointer from integer without a cast
- 【warning: assignment makes pointer from integer without a cast 】
- warning: assignment makes pointer from integer without a cast
- warning: assignment makes pointer from integer without a cast
- 关于 warning: assignment makes pointer from integer without a cast
- warning: assignment makes pointer from integer without a cast
- assignment makes integer from pointer without a cast
- warning: assignment makes pointer from integer without a cast
- warning: assignment makes pointer from integer without a cast。
- warning: assignment makes pointer from integer without a cast
- linux c 编译时出现warning: assignment makes pointer from integer without a cast原因
- 警告: assignment makes pointer from integer without a cast的另外一种原因
- 关于如何解决warning: assignment makes pointer from integer without a cast的警告
- Flex: yylval warning: assignment makes pointer from integer without a cast [enabled by default]
- inet_ntoa函数在编译时候报 ”warning: assignment makes pointer from integer without a cast“ 告警
- warning: initialization makes pointer from integer without a cast
- Android Animations动画使用详解
- rmdir命令
- Apache cxf rpc demo
- linux 查找大文件
- linuxc多进程与外部程序调用(转载)
- gcc_warning:assignment makes integer from pointer without a cast
- LAMP环境的搭建
- 四件在我步入职业软件开发生涯那天起就该知道的事情
- 开博第一篇:博客的初衷
- WinCE平台NKDbgPrintfW等支持的格式符及浮点数打印方法
- xcode5 cocos2d-x (三) CCSprite 精灵类的常见创建方法
- 心脏支架手术后还是胸闷气短,八一八我支架手术后的苦逼生活
- 使用SQL*PLUS,构建完美excel或html输出
- Java NIO框架Netty教程(一) – Hello Netty