我的程序有以下警告,因此我认为显示了一个核心转储分段错误错误:

warning: passing argument 2 of ‘getaddrinfo’ makes pointer from integer without a cast [enabled by default]

rc = getaddrinfo(svrHost, svrPort, &hints, &res);
                           ^
In file included from client6_main.c:16:0:
/usr/include/netdb.h:662:12: note: expected ‘const char * restrict’ but argument is of type ‘short unsigned int’
extern int getaddrinfo (const char *__restrict __name,


代码段是:

rc = getaddrinfo(svrHost, svrPort, &hints, &res);

if(rc != 0){
    printf("Host not found --> %s\n", gai_strerror(rc));
    if (rc == EAI_SYSTEM)
        perror("getaddrinfo() failed");
}


请指导

最佳答案

您需要将服务名称或端口号作为十进制字符串作为getaddrinfo调用的第二个参数。

为此,只需将变量svrPort的类型从unsigned short更改为char*char const*,并使其指向有效的字符串即可。

例如,用"http""80"代替80

关于c - “getaddrinfo”使用整数进行指针转换而无需强制转换[默认启用],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36820849/

10-11 17:23