警告:格式“%p"需要“void *"类型的参数,但参数 3 的类型为“char **" [英] warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘char **’

查看:79
本文介绍了警告:格式“%p"需要“void *"类型的参数,但参数 3 的类型为“char **"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此时我正在尝试在我的代码中打印出 argv 的值,但是当我尝试编译它时,我得到警告:格式‘%p’需要‘void *’类型的参数,但参数3的类型为‘char **.我不确定这个警告是什么意思.尽管 %p 是一个指针,但它不打算用于 argv 吗?

I'm trying to print out the value of argv at this point in my code, but when I try to compile it, I get warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘char **. I'm not sure what this warning means though. Is %p not meant to be used for argv even though it's a pointer?

int main(int argc, char* argv[])
{
    printf("%s%p", "argv = ", argv);
}

推荐答案

那你为什么不投呢?

printf("argv = %p\n", (void *)argv);`

指定了 %p 指针(POSIX <代码>printf(); C11 §7.21.6.1 fprintf 函数):

The %p pointer is specified (POSIX printf(); C11 §7.21.6.1 The fprintf function):

p — 参数应该是一个指向 void 的指针.指针的值以实现定义的方式转换为打印字符序列.

p — The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

由于char ** 不是void *,所以需要进行显式转换.标准不允许编译器为您转换为 void *printf() 声明中的省略号表示发生默认参数提升,但这仅影响 float 类型和小于 int 的整数类型(shortchar 等).

Since char ** is not a void *, you need to do the explicit conversion. The compiler isn't allowed by the standard to do the conversion to void * for you — the ellipsis notation in the declaration of printf() means that default argument promotions occur, but that affects only the float type and integer types smaller than int (short, char, etc).

在如今的大多数机器上,这是一个空操作.在我学习用 C 编程的(长期过时)机器上,还没有 void *(对于标准来说太旧了),但等效的是 char *,并且给定内存位置的 char * 地址的值与任何其他类型的指针值不同 - 强制转换不是可选的.

On most machines these days, that's a no-op. On the (long obsolete) machine where I learned to program in C, there weren't void * yet (too old for the standard), but the equivalent was char *, and the value of a char * address for a given memory location was different from the pointer value for any other type — the casts were not optional.

请注意,我假设您打算打印地址的值.如果您打算打印内容,那么还有更多工作要做.

Note that I'm assuming you intend to print the value of the address. If you intend to print the contents, then there's more work to be done.

这篇关于警告:格式“%p"需要“void *"类型的参数,但参数 3 的类型为“char **"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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