Something about inet_ntoa ()

I write a server using the function char* inet_ntoa(struct in_addr in),When I turned on the header <sys/socket.h>and <netinet/in.h>, binary executable can be generated using compiler warnings, but a segment error occurs when the program processes the returned string from inet_ntoa. But when I added the title <arpa/inet.h>,, everything looks fine.

What's the matter?

+5
source share
1 answer

arpa/inet.hcontains an ad char* inet_ntoa(struct in_addr in). If you do not include this header, your compiler will use an implicit declaration int inet_ntoa(). Incorrect declaration can easily lead to segfault, especially if you are on a system where sizeof(int)!=sizeof(void*).

gcc, -Wall. gcc .

+12

All Articles