2013-05-02 106 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) { } 
相關問題