2013-05-02 108 views
2

编译器针对以下代码段显示以下警告。请帮我纠正它。为什么我会收到这些警告?

如果((tmp_n =(结构点*)的shmat(shm_net,NULL,0))==(INT *)-1){}
警告:不同指针类型的比较缺乏的铸造[启用为预设]

它的一个C程序,此代码段为共享存储器段附接至指针** tmp_n它是类型为struct点。

struct dot {int weight; int tmv;};

+0

我不确定该警告消息可以清晰多少。您正在比较不同的指针类型,而不是先将它们转换为常用类型。 – Cairnarvon 2013-05-02 06:38:13

回答

3

试试这个

if((tmp_n = (struct dot *)shmat(shm_net, NULL, 0)) == (void *) -1) { } 

,并期待在man-page,它指出:

Return Value 
On success shmat() returns the address of the attached shared memory segment; 
on error (void *) -1 is returned, and errno is set to indicate the cause of the error. 
+0

不客气!如有疑问,请考虑手册页! – 2013-05-06 11:32:40

+0

但(int *)-1在我的其他程序中没有任何警告。也许我在第一时间写错了,但是因为它在工作,而且我一直在使用它很长时间,所以我忘了在发生这种情况时查阅手册。 – 2013-05-06 11:41:54

+0

可能是因为在你的其他程序中隐式投射工作... – 2013-05-06 11:43:12

2

你需要转换-1以相同的指针类型,你与比较变量:

if((tmp_n = (struct dot *)shmat(shm_net, NULL, 0)) == (struct dot *) -1) { } 
相关问题