#include <netdb.h>
#include <stdio.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
char *ptr = NULL;
struct hostent *hptr = NULL;
struct in_addr *hipaddr = NULL;
ptr = argv[1];
inet_aton(ptr,hipaddr);
hptr = gethostbyaddr(hipaddr, 4, AF_INET);
printf("official hostname:%s\n",hptr->h_name);
return 0;
}
运行结果显示:segmentation fault (core dumped)。但我不知道如何使它工作。所以我需要一些帮助......
我将代码更改为:
#include <netdb.h>
#include <stdio.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
char *ptr = NULL;
struct hostent *hptr = NULL;
struct in_addr hipaddr ;
ptr = argv[1];
inet_aton(ptr,&hipaddr);
hptr = gethostbyaddr(&hipaddr, sizeof(hipaddr), AF_INET);
printf("official hostname:%s\n",hptr->h_name);
return 0;
}
然后它起作用了!但为什么呢?