调用 inet_ntoa 函数产生的段错误 “Segmentation fault (core dumped)” 解决办法
struct hostent *host = gethostbyname("www.google.com");
...
printf("ip: %s\n", inet_ntoa(*((struct in_addr*)host->h_addr)));
执行到 inet_ntoa 时会产生段错误:
Segmentation fault (core dumped)
【缘由】
听说是在64位环境下会产生错误,而在32位系统下运行正常。
【解决办法】
加入头文件:
#include <arpa/inet.h>
编译后执行正常。函数