0

对于以下代码段:

    /* get IP address on a specific network interface */
    void get_ip_dev(char* ipaddr, char* interface)
    {
            int fd;
            struct ifreq ifr;
            fd = socket(AF_INET, SOCK_DGRAM, 0);
            ifr.ifr_addr.sa_family = AF_INET;
            strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
            ioctl(fd, SIOCGIFADDR, &ifr);
            close(fd);
            memcpy(ipaddr, inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr), 20);
    }

我收到来自 的警告memcpy,为什么?谢谢!

network.c: In function ‘get_ip_dev’:
network.c:39:43: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:44:14: note: expected ‘const void * __restrict__’ but argument is of type ‘int’
4

2 回答 2

2

这应该是一个简单的解决方法:您需要包含<arpa/inet.h>. 编译器可能假设它返回 int。

于 2013-06-13T00:05:49.923 回答
0

这是因为第二个参数应该是地址位置。在您的情况下,它是价值本身。

于 2014-11-19T09:21:15.573 回答