inet_ntoa函数在编译时候报 ”warning: assignment makes pointer from integer without a cast“ 告警

编译有告警代码

#include 
#include 
#include 
#include 

/*

    char *inet_ntoa(struct in_addr in)
    int inet_aton(const char *cp, struct in_addr *inp);
    // struct sockaddr_in server_ip;

*/

int main()
{
    int ret = 0;
    char *ip_addr = NULL;
    struct in_addr byte_addr = {0};
    const char ipaddress[] = "192.168.1.5";

    ret = inet_aton(ipaddress, &byte_addr);
    printf("inet_aton return %d\n", ret);
    printf("byte addr is (0x%x)\n", byte_addr.s_addr);
    printf("byte 0 value[0x%x]\n", *((unsigned char *)&byte_addr.s_addr));
    printf("byte 1 value[0x%x]\n", *((unsigned char *)&byte_addr.s_addr + 1));
    printf("byte 2 value[0x%x]\n", *((unsigned char *)&byte_addr.s_addr + 2));
    printf("byte 3 value[0x%x]\n", *((unsigned char *)&byte_addr.s_addr + 3));

    /* 将网络字节序的IP地址再转换为字符串形式*/
    ip_addr = inet_ntoa(byte_addr);
    printf("ip address [%s]\n", ip_addr);
    
    return 0;
}


编译结果:

gcc -E server.c -o server.i
gcc -S server.c -o server.s
server.c: In function 'main':
server.c:30:13: warning: assignment makes pointer from integer without a cast
gcc -g server.c -o server
server.c: In function 'main':
server.c:30:13: warning: assignment makes pointer from integer without a cast


很奇怪为什么会报告警

inet_ntoa函数原型

/* Convert Internet number in IN to ASCII representation.  The return value
   is a pointer to an internal array containing the string.  */
extern char *inet_ntoa (struct in_addr __in) __THROW;

该定义在 arpa/inet.h中,所以在代码中包含这个头文件,再次进行编译,无告警

gcc -E server.c -o server.i
gcc -S server.c -o server.s
gcc -g server.c -o server

已经没有告警;应该是没有包含正确的头文件,导致引用了错误的原型声明(没有找到这个错误的原型声明在哪个头文件中)


你可能感兴趣的:(一万行代码)