Linux 关于inet_ntoa()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11049687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
something about inet_ntoa()
提问by venus.w
I write a server using the function char* inet_ntoa(struct in_addr in),When I included the header
<sys/socket.h>and <netinet/in.h>,an executable binary can be generated with compiler warnings, but a segment fault happens, when the program handle the return string from inet_ntoa. But when I added the header <arpa/inet.h>,everything seems ok.
我使用函数写一个服务器char* inet_ntoa(struct in_addr in),当我包括报头
<sys/socket.h>和<netinet/in.h>,可以用编译器警告生成可执行的二进制,但段发生故障,当程序手柄从返回字符串inet_ntoa。但是当我添加标题时,<arpa/inet.h>,一切似乎都没问题。
What's the matter?
怎么了?
采纳答案by Piotr Praszmo
arpa/inet.hcontains the declaration of char* inet_ntoa(struct in_addr in). If you don't include this header your compiler will use implicit declaration int inet_ntoa(). Wrong declaration can easily lead to segfault, especially if you are on system where sizeof(int)!=sizeof(void*).
arpa/inet.h包含 的声明char* inet_ntoa(struct in_addr in)。如果您不包含此头文件,您的编译器将使用隐式声明int inet_ntoa()。错误的声明很容易导致段错误,特别是如果你在系统 where sizeof(int)!=sizeof(void*).
If you are using gccyou can add -Wallflag. gccwill warn you about using functions without explicit declaration.
如果您正在使用,gcc您可以添加-Wall标志。gcc将警告您使用没有显式声明的函数。

