简体   繁体   English

警告:格式'%s'需要类型'char *',但参数2的类型为'int'

[英]warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

I think this code and error is self-explanatory, but I don't know why? 我认为这段代码和错误是不言自明的,但我不知道为什么?

Environment: 环境:
OS: Mac OS X 10.6.1 操作系统:Mac OS X 10.6.1
Compiler: i686-apple-darwin10-gcc-4.2.1 编译器:i686-apple-darwin10-gcc-4.2.1

code: 码:

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  #include <netdb.h>
 4  #include <sys/socket.h>
 5  
 6  int 
 7  main(int argc, char **argv)
 8  {
 9      char           *ptr, **pptr;
10      struct hostent *hptr;
11      char            str[32];
12  
13      //ptr = argv[1];
14      ptr = "www.google.com";
15  
16      if ((hptr = gethostbyname(ptr)) == NULL) {
17          printf("gethostbyname error for host:%s\n", ptr);
18  
19      }
20      printf("official hostname:%s\n", hptr->h_name);
21  
22      for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
23          printf(" alias:%s\n", *pptr);
24  
25      switch (hptr->h_addrtype) {
26      case AF_INET:
27      case AF_INET6:
28          pptr = hptr->h_addr_list;
29  
30          for (; *pptr != NULL; pptr++)
31              printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
32          break;
33      default:
34          printf("unknown address type\n");
35          break;
36      }
37      return 0;
38  }


compiler and executed output below: 编译器和执行输出如下:

zhumatoMacBook:CProjects zhu$ gcc gethostbynamedemo.c 
gethostbynamedemo.c: In function ‘main’:
gethostbynamedemo.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
zhumatoMacBook:CProjects zhu$ ./a.out 
official hostname:www.l.google.com
 alias:www.google.com
Segmentation fault

Why am I getting the format warning and could this be the cause of the segmentation fault? 为什么我会收到格式警告,这可能是分段错误的原因?

  1. Please compile your code using -Wall. 请使用-Wall编译代码。
  2. include header file for inet_ntop (arpa/inet.h) 包含inet_ntop的头文件(arpa / inet.h)
  3. read inet_ntop(3) man page and be careful about parameter types. 阅读inet_ntop(3)手册页并注意参数类型。

If I count right, the warning is emitted for this line: 如果我算数正确,则会为此行发出警告:

printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

According to this page , inet_ntop does indeed return char* . 根据这个页面inet_ntop确实返回char* However, apparently you don't include <arpa/inet.h> - this can cause this warning, as the compiler may default to interpret an undeclared function as one returning an int. 但是,显然你没有包含<arpa/inet.h> - 这可能会导致出现此警告,因为编译器可能默认将未声明的函数解释为返回int的函数。

Next time, please mark the problematic code line(s) with eg a comment - it would increase your chances of getting useful answers :-) 下次,请用例如注释标记有问题的代码行 - 这会增加获得有用答案的机会:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:格式%s期望为char *类型,但参数2为int类型 - warning: format %s expects type char * but argument 2 has type int 警告:格式&#39;%c&#39;需要类型&#39;int&#39;,但参数2的类型为&#39;char *&#39; - warning: format '%c' expects type 'int', but argument 2 has type 'char *' 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ 格式为&#39;%s&#39;,参数类型为&#39;char *&#39;,但是参数2为类型&#39;int&#39; - format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ 警告:格式&#39;%s&#39;期望参数类型为&#39;char *&#39;,但是参数2使用argv类型为&#39;int&#39; - warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ using argv 警告:格式“%s”需要类型为“char *”的参数,但参数 2 的类型为“int” - warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ 格式&#39;%s&#39;期望参数类型为&#39;* char&#39;,但参数2的类型为&#39;int&#39; - format '%s' expects argument of type '*char' but argument 2 has type 'int' 警告:格式&#39;%c&#39;需要&#39;char *&#39;类型的参数,但参数3的类型为&#39;int&#39; - warning: format '%c' expects argument of type 'char *', but argument 3 has type 'int' C-“警告:格式”%c”期望参数类型为“ char *”,但是参数3的类型类型为“ int”: - C - “warning: format ”%c“ expects argument of type ”char *“, but argument 3 has type ”int":? GCC编译器警告:格式&#39;%c&#39;需要类型为&#39;char *&#39;的参数,但参数2为类型&#39;int *&#39;[-Wformat] - GCC compiler warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM