gpt4 book ai didi

c - 为什么我会收到这些警告?

转载 作者:行者123 更新时间:2023-12-02 05:05:32 24 4
gpt4 key购买 nike

编译器针对以下代码段显示以下警告。请帮我改正。

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

它是一个 C 程序,该代码段用于将共享内存段附加到指针 **tmp_n,其类型为 struct dot。

结构点 {int weight; int tmv;};

最佳答案

试试这个

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.

关于c - 为什么我会收到这些警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16331809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com