【问题标题】:warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [enabled by default]警告:传递“memcpy”的参数 2 使指针从整数不进行强制转换 [默认启用]
【发布时间】:2013-06-09 06:20:47
【问题描述】:

对于以下sn-p:

    /* 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’

【问题讨论】:

    标签: c memcpy strcpy


    【解决方案1】:

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

    【讨论】:

    • @misteryes 如果编译器看到一个新函数但没有看到它的定义,它假定它返回一个 int。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-20
      • 2015-05-30
      • 1970-01-01
      • 2017-05-22
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多