調用inet_ntoa產生 segmentation fault解決

調用 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>

編譯後執行正常。函數

相關文章
相關標籤/搜索