gpt4 book ai didi

c - 如何在 C 中打印变量地址?

转载 作者:太空狗 更新时间:2023-10-29 16:19:50 27 4
gpt4 key购买 nike

当我运行这段代码时。

#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\n", &x, x);
printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
moo(9, y);
}

void moo(int a, int *b)
{
printf("Address of a = %d, value of a = %d\n", &a, a);
printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}

我的编译器中不断出现此错误。

/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 打印一个指针。来自规范:

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.

并且不要忘记类型转换,例如

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

关于c - 如何在 C 中打印变量地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5286451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com