سؤال

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