c指针 - 警告格式'%x'需要'unsigned int'类型的参数

时间:2012-03-14 21:53:48

标签: c compiler-errors x86

我正在阅读Hacking剥削艺术,并且有一个例子,我似乎无法正确。试图在错误中编译结果:

./addressof.c: In function ‘main’:
./addressof.c:8:4: warning: format ‘%x’ expects argument of type ‘unsigned int’,
but argument 2 has type ‘int *’ [-Wformat]


#include <stdio.h>
int main() {
   int int_var = 5;
   int *int_ptr;

   int_ptr = &int_var; // Put the address of int_var into int_ptr.

   printf("int_ptr = 0x%08x\n", int_ptr);
   printf("&int_ptr = 0x%08x\n", &int_ptr);
   printf("*int_ptr = 0x%08x\n\n", *int_ptr);

   printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
   printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n",
      &int_ptr, int_ptr, *int_ptr);
}

我明白错误的位置,我只是不确定如何解决这个问题。

1 个答案:

答案 0 :(得分:16)

指针的格式说明符是%p,而不是%x。 (见here