簡體   English   中英

為什么會收到這些警告?

[英]Why do I get these warnings?

對於以下代碼段,編譯器顯示以下警告。 請幫我改正。

if((tmp_n =(結構點*)shmat(shm_net,NULL,0))==(int *)-1){}
警告:不同指針類型的比較缺少強制轉換[默認啟用]

它是一個C程序,該代碼段用於將共享內存段附加到指針** tmp_n,該指針的類型為struct dot。

struct dot {int weight; int tmv;};

試試這個

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

並查看手冊頁 ,其中指出:

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. 

您需要將-1轉換為與要比較的變量相同的指針類型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM