gethostbyname and structs
I'm trying to some something that should be pretty simple; use the gethostbyname()-function. My problem is that I don't understand what the struct is doing, and why I need it. Structs in general makes me confused. For example, I tried with this code:
Code:
#include <stdio.h>
#include <netdb.h>
extern int h_errno;
int main() {
char *name = "192.168.0.1";
struct hostent *hp;
if((hp = gethostbyname(name)) == NULL){
herror("gethostbyname");
}
return 0;
}
It compiles as it should, but I want to print out the name of the host - but where is it stored? I know the hostent struct has a field called '*h_name', which holds the name of the host/server. But I can't print out the name by doing 'hp.h_name' or by just trying to print out the variable 'name'. So my questions are; how does the info get into (or point to) 'h_name'? How do I use the struct? Do I have to declare the whole struct to use it?
And, the declaration of the variable 'name', is it illigal? Do I use memory I haven't allocated?