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

标签 c compiler-errors x86

我目前正在阅读 Hacking the art of exploitation,其中有一个我似乎无法正确理解的示例。尝试编译导致错误:

./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);
}

我知道错误在哪里,我只是不确定如何解决这个问题。

最佳答案

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

关于c 指针 - 警告格式 '%x' 需要类型为 'unsigned int' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710857/

相关文章:

c - 编辑此汇编代码以仅使用加/减/移位

c - 了解Linux中的膜垒函数

c - 没有规则可以创建 `main.c' 所需的目标 `main.o' 。停止

c - 线程例程内的 malloc

java - 未为参数类型定义运算符+字符串,无效

assembly - 如何指定 x86 和 PowerPC 指令的执行时间?

c - 分配和序列点 : how is this ambiguous?

c - ATMEGA 2560 uart 代码未在 minicom 上给出正确的输出

ios - 函数指针声明编译错误

java - 为什么这个组合代码不起作用?