简体   繁体   中英

Why do I get these warnings?

The compiler showed the following warnings for the following code segment. Please help me correct it.

if((tmp_n = (struct dot *)shmat(shm_net, NULL, 0)) == (int *) -1) { }
warning: comparison of distinct pointer types lacks a cast [enabled by default]

Its a C program, this code segment is for attaching a shared memory segment to a pointer **tmp_n which is of the type struct dot.

struct dot {int weight; int tmv;};

Try this one

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

and look at the man-page , it states:

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) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM