对于以下片段:
/* 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’
最佳答案
这应该是一个简单的修复:您需要包含 <arpa/inet.h>
.编译器可能假设它返回 int。
关于c - 警告 : passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077341/