我刚刚开始使用 C 进行编程。这应该是一个简单的程序,但我遇到了分段错误。我会很感激任何帮助
问候,
胡安
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc, char **argv)
{
struct hostent *hp;
struct in_addr **addr_list;
if ((hp = gethostbyname("www.yahoo.ca")) == NULL)
{
printf("gethostbyname() failed\n");
}
else
{
printf("Official name = %s\n", hp->h_name);
addr_list = (struct in_addr **)hp->h_addr_list;
unsigned int i = 0;
while (addr_list[i] != NULL)
{
printf("%s\n",inet_ntoa(*((struct in_addr *)hp->h_addr)));
i++;
}
}
}
以下是程序的调用方式:
administrator@ubuntu:~/Documents$ ./a.out
Official name = any-rc.a01.yahoodns.net
Segmentation fault (core dumped)
administrator@ubuntu:~/Documents$