如何在C中打印变量地址? [英] How to print variable addresses in C?
本文介绍了如何在C中打印变量地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行此代码时.
#include <stdio.h>
void moo(int a, int *b);
int main()
{
int x;
int *y;
x = 1;
y = &x;
printf("Address of x = %d, value of x = %d
", &x, x);
printf("Address of y = &d, value of y = %d, value of *y = %d
", &y, y, *y);
moo(9, y);
}
void moo(int a, int *b)
{
printf("Address of a = %d, value of a = %d
", &a, a);
printf("Address of b = %d, value of b = %d, value of *b = %d
", &b, b, *b);
}
我的编译器不断收到这个错误.
I keep getting this error in my compiler.
/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
你能帮我吗?
谢谢
吹牛
推荐答案
您想使用 %p
打印一个指针.来自规范:
You want to use %p
to print a pointer. From the spec:
p
参数应该是一个指向 void
的指针.指针的值以实现定义的方式转换为打印字符序列.
p
The argument shall be a pointer tovoid
. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
不要忘记演员表,例如
printf("%p
",(void*)&a);
这篇关于如何在C中打印变量地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文