Domanda

Sto cercando di utilizzare la funzione gethostbyname, ma il mio codice:

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

    struct hostent *hp = gethostbyname(hostname);

    ...

}

continua a tornare:

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

Qualcuno sa che cosa c'è di sbagliato con la mia sintassi?

grazie

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top