警告:格式'%s'的预期中键入'字符*',但参数2的类型'诠释' [英] warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

查看:159
本文介绍了警告:格式'%s'的预期中键入'字符*',但参数2的类型'诠释'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这$​​ C $ c和错误是不言自明的,但我不知道为什么?


环境:

操作系统:Mac OS X 10.6.1

编译:i686的-苹果darwin10-GCC-4.2.1


code:

  1的#include<&stdio.h中GT;
 2#包括LT&;&stdlib.h中GT;
 3#包括LT&;&netdb.h中GT;
 4#包括LT&; SYS / socket.h中>
 五
 6 INT
 7主(INT ARGC,字符** argv的)
 8 {
 9字符* PTR,** PPTR;
10结构hostent * hptr;
11字符海峡[32];
12
13 // PTR =的argv [1];
14 PTR =www.google.com;
15
16,如果((hptr =的gethostbyname(PTR))== NULL){
17的printf(主机的gethostbyname错误:%s \\ n,PTR);
18
19}
20的printf(官方主机名:%S \\ n,hptr-> h_name);
21
22(PPTR = hptr-> h_aliases;!* PPTR = NULL; PPTR ++)
23的printf(别名:%S \\ n,* PPTR);
24
25开关(hptr-> h_addrtype){
26的情况下AF_INET:
27的情况下AF_INET6:
28 PPTR = hptr-> h_addr_list;
29
30(* PPTR = NULL;!PPTR ++)
31的printf(地址:%S \\ n,inet_ntop(hptr-> h_addrtype,* PPTR,海峡,sizeof的(STR)));
32休息;
33默认:
34的printf(未知地址类型\\ n);
35休息;
36}
37返回0;
38}



编译器及以下执行的输出:

  zhumatoMacBook:CProjects朱$ GCC gethostbynamedemo.c
gethostbynamedemo.c:在函数'主':
gethostbynamedemo.c:31:警告:格式'%s'的预期中键入'字符*',但参数2的类型'诠释'
zhumatoMacBook:CProjects朱$ ./a.out
正式的主机名:www.l.google.com
 别名:www.google.com
分段故障

为什么我收到的格式警告,难道这是段错误的原因是什么?


解决方案

  1. 请编译使用-Wall您code。

  2. 包括inet_ntop头文件(ARPA / inet.h)

  3. 阅读inet_ntop(3)手册页​​和小心参数类型。

I think this code and error is self-explanatory, but I don't know why?

Environment:
OS: Mac OS X 10.6.1
Compiler: 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.
  2. include header file for inet_ntop (arpa/inet.h)
  3. read inet_ntop(3) man page and be careful about parameter types.

这篇关于警告:格式'%s'的预期中键入'字符*',但参数2的类型'诠释'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆