打印指针有问题每次尝试编译下面的程序时,都会出现以下错误:
pointers.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int *’
很明显,我在这里遗漏了一些简单的东西,但是从我看到的其他类似代码的例子来看,这应该是可行的。
这是密码,任何帮助都很好!
#include <stdio.h>
int main(void)
{
int x = 99;
int *pt1;
pt1 = &x;
printf("Value at p1: %d\n", *pt1);
printf("Address of p1: %p\n", pt1);
return 0;
}
最佳答案
只需将int指针转换为空指针:
printf( "Address of p1: %p\n", ( void * )pt1 );
您的代码是安全的,但是您正在使用
-Wformat
警告标志进行编译,该标志将键入check对printf()
和scanf()
的调用。