ramnarayanan
ramnarayanan

Reputation: 117

‘getaddrinfo’ makes pointer from integer without a cast [enabled by default]

My program has the following warning because of which I think there is a core dump segmentation fault error showing up:

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,

The code segment is:

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");
}

Please guide

Upvotes: 0

Views: 1946

Answers (2)

Uri Brecher
Uri Brecher

Reputation: 437

The first two function arguments are expected to be pointers to strings.

int getaddrinfo(const char *node, 
                const char *service,
                const struct addrinfo *hints,
                struct addrinfo **res);

I assume from the warning you got that svrPort is of type int. The compiler will treat this in as a pointer. If svrPort is initialized to 1234 then the function will lookup a string starting at address 1234 which may or may not be a valid address.

You should probably pass NULL or 0 to tell getaddrinfo to ignore this argument.

Upvotes: 1

rems4e
rems4e

Reputation: 3172

You need to put the service name or the port number as a decimal string as second parameter of the getaddrinfo call.

For that, just change the type of the variable svrPort from unsigned short to char* or char const*, and make it point to a valid string.

For example, "http" or "80" instead of 80.

Upvotes: 1

Related Questions