我有一段代码看起来像
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct hostent *server;
server = gethostbyname(argv[1]);
struct in_addr * address=(in_addr * )server->h_addr;
fprintf(stderr,"[%d] [%s] server [%s] \n",__LINE__,__func__,inet_ntoa(*address));
}
当我使用 g++ 编译器时,它得到了编译,但同样给出了 gcc 编译器的以下错误
myclient.c: In function ‘main’:
myclient.c:10:31: error: ‘in_addr’ undeclared (first use in this function)
myclient.c:10:31: note: each undeclared identifier is reported only once for each function it appears in
myclient.c:10:41: error: expected expression before ‘)’ token
我是不是漏掉了什么?
最佳答案
您应该在 c
代码中使用 struct in_addr
,而不仅仅是 in_addr
(在转换时也是如此)。
关于c++ - C和C++的编译区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986122/