一、warning: cast from pointer to integer of different size
1).while((sp < 256) && (src[sp] != NULL))
->while((sp < 256) && (src[sp] != '\0'))
2).dest[dp] = NULL;
dest[dp] = '\0';
为了测试字符串的实际长度,C语言规定了字符串结束标志'\n';'\n'代表ASCII码为0的字符,它不是一个可以显示
的字符,而是一个空操作符,只作为一个辨别的标志。
二、warning: dereferencing type-punned pointer will break strict-aliasing rules
in file CSTFlowControl.c:
STAT_FC_STREAMING_FCSTATUS_EX * pMsg;
STREAM_PACKET strPkt;
pMsg = (STAT_FC_STREAMING_FCSTATUS_EX *)&strPkt;
STAT_FC_STREAMING_FCSTATUS_EX * pMsg;
STREAM_PACKET strPkt;
pMsg = (STAT_FC_STREAMING_FCSTATUS_EX *)&strPkt;
when running C:\WindRiver\gnu\4.1.2-vxworks-6.6/x86-win32/bin/ccmips
(gcc version 4.1.2 (Wind River VxWorks G++ 4.1-82))
get warning: dereferencing type-punned pointer will break strict-aliasing rules
(gcc version 4.1.2 (Wind River VxWorks G++ 4.1-82))
get warning: dereferencing type-punned pointer will break strict-aliasing rules
solution:
pMsg = (STAT_FC_STREAMING_FCSTATUS_EX *) (int32)&strPkt;
pMsg = (STAT_FC_STREAMING_FCSTATUS_EX *) (int32)&strPkt;
explanation:
convert the address of strPkt to an integer, then convert this integer to an pointer to struct (STAT_FC_STREAMING_FCSTATUS_EX
you should confirm that the conversion between those two data type is legal.
convert the address of strPkt to an integer, then convert this integer to an pointer to struct (STAT_FC_STREAMING_FCSTATUS_EX
you should confirm that the conversion between those two data type is legal.
refer to:
我们已经知道,指针的值就是指针指向的地址,在32位程序中,指针的值其实是一个32位整数。那可不可以把一个整数当作指针的值直接赋给指针呢?就象下面的语句:
unsigned int a;
TYPE *ptr;//TYPE是int,char或结构类型等等类型。
...
...
a=20345686;
ptr=20345686;//我们的目的是要使指针ptr指向地址20345686(十进制)
ptr=a;//我们的目的是要使指针ptr指向地址20345686(十进制)
编译一下吧。结果发现后面两条语句全是错的。那么我们的目的就不能达到了吗?不,还有办法:
unsigned int a;
TYPE *ptr;//TYPE是int,char或结构类型等等类型。
...
...
a=某个数,这个数必须代表一个合法的地址;
ptr=(TYPE*)a;//呵呵,这就可以了。
严格说来这里的(TYPE*)和指针类型转换中的(TYPE*)还不一样。这里的(TYPE*)的意思是把无符号整数a的值当作一个地址来看待。
上面强调了a的值必须代表一个合法的地址,否则的话,在你使用ptr的时候,就会出现非法操作错误。
想想能不能反过来,把指针指向的地址即指针的值当作一个整数取出来。完全可以。下面的例子演示了把一个指针的值当作一个整数取出来,然后再把这个整数当作一个地址赋给一个指针:
例十六:
int a=123,b;
int *ptr=&a;
char *str;
b=(int)ptr;//把指针ptr的值当作一个整数取出来。
str=(char*)b;//把这个整数的值当作一个地址赋给指针str。
例十六:
int a=123,b;
int *ptr=&a;
char *str;
b=(int)ptr;//把指针ptr的值当作一个整数取出来。
str=(char*)b;//把这个整数的值当作一个地址赋给指针str。
好了,现在我们已经知道了,可以把指针的值当作一个整数取出来,也可以把一个整数值当作地址赋给一个指针。
对程序要求严格的话可以用 -Wall -Werror