2
我剛開始使用C編程。這應該是一個簡單的程序,但我得到了分段錯誤。我將不勝感激任何HLEP使用gethostbyname
問候,
胡安
#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++;
}
}
}
這裏是如何將節目叫:
[email protected]:~/Documents$ ./a.out
Official name = any-rc.a01.yahoodns.net
Segmentation fault (core dumped)
[email protected]:~/Documents$
在哪一點你會得到seg故障?寫代碼不會幫助... – Anshul 2013-03-28 04:30:17