0

for the following snippet:

    /* 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);
    }

I get a warning from memcpy, why? thanks!

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’
misteryes
  • 2,167
  • 4
  • 32
  • 58

2 Answers2

2

It should be a simple fix: you need to include <arpa/inet.h>. The compiler is probably assuming it returns int.

ctn
  • 2,887
  • 13
  • 23
  • 1
    @misteryes If the compiler sees a new function but hasn't seen its definition it assumes it returns an int. – ctn Jun 13 '13 at 00:13
0

This is because the second argument is expected to be an address location. In your case it is the value itself.