문제

I'm trying to use the function gethostbyname, but my code:

int handleTCP(char *hostname, char* portNo){

    struct hostent *hp = gethostbyname(hostname);

    ...

}

Keeps returning:

21: warning: initialization makes pointer from integer without a cast

Does anyone know what is wrong with my syntax?

thanks

도움이 되었습니까?

해결책

You forgot to #include <netdb.h>. Because you didn't include this file, you are running into the "default int" rule. Basically, in C, if a function has no prototype, it is assumed to be:

int function_name(); in other words "returns an int, takes unknown number of parameters".

Properly declaring the function prototype (in this case by including the header) will avoid this.

다른 팁

I think you forgot to include netdb.h header file, so that compiler knows nothing about gethostbyname function and assumes that it returns integer. Then it complaints because in that case you would have converted integer to a pointer. You need to include a header to provide compiler with correct type information.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top