Search code examples
csocketstcpgethostbyname

How to use gethostbyname in c


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


Solution

  • 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.