printf指针参数类型警告? [英] printf pointer argument type warning?

查看:79
本文介绍了printf指针参数类型警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有摆脱以下警告的好方法?我知道这是一个类型问题,因为我传递的是 unsigned long指针而不是 unsigned long ,但是确实printf以某种方式支持将指针作为参数?我心目中的学童想摆脱这个警告。如果不是,您如何使用 printf 打印取消引用的指针值?

Is there a good way to get rid of the following warning? I know it's a type issue in that I'm passing a unsigned long pointer and not an unsigned long, but does printf somehow support pointers as arguments? The pedantic in me would like to get rid of this warning. If not, how do you deal with printing de-referenced pointer values with printf?

#include <stdio.h>

int main (void) {
    unsigned long *test = 1;
    printf("%lu\n", (unsigned long*)test);
    return 0;
}

警告:格式指定类型 unsigned long,但参数具有类型

推荐答案

unsigned long *test = 1;

无效C。如果要使用指向值<$ c $的对象的指针c> 1 ,您可以执行以下操作:

is not valid C. If you want to have a pointer to an object of value 1, you can do:

unsigned long a = 1;
unsigned long *test = &a;

或使用C99复合文字:

or using a C99 compound literal:

unsigned long *test = &(unsigned long){1UL};

现在还:

printf("%lu\n", (unsigned long*)test);

不正确。您实际上想要:

is incorrect. You actually want:

printf("%lu\n", *test);

打印 unsigned long 的值对象 * test

打印 test 指针值(以实现定义的方式),您需要:

To print the test pointer value (in an implementation-defined way), you need:

printf("%p\n", (void *) test);

这篇关于printf指针参数类型警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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