我浏览了与我的问题匹配的问题,但找不到答案。创建程序以显示整数“ i”和“ k”的存储位置后,它没有使用clang进行编译。使用SoloLearn的IDE时,效果很好。
#include <stdio.h>
void test(int k);
int main() {
int i = 0;
printf("The address of i is %x\n", &i);
test(i);
printf("The address of i is %x\n", &i);
test(i);
return 0;
}
void test(int k) {
printf("The address of k is %x\n", &k);
}
这些是我遇到的错误。
memory.c:8:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
printf("The address of i is %x\n", &i);
~~ ^~
memory.c:10:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
printf("The address of i is %x\n", &i);
~~ ^~
memory.c:17:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
printf("The address of k is %x\n", &k);
~~ ^~
3 warnings generated.
我需要在整数上签名吗?如果是的话,我该怎么办?
答案 0 :(得分:2)
如果要打印变量或存储位置的地址,则应使用%p
格式说明符。例如
int i = 0;
printf("The address of i is %p\n", (void*)&i);/* %p format specifier expects argument of void* */
根据C
标准:
(C11,7.21.6.1p8格式化的输入/输出功能)“ p参数 应该是指向void的指针。”