c - 跨平台使用 void 指针

标签 c pointers void-pointers format-specifiers

我听说应该首先将指针转换为 void 以确保跨平台值的一致性,并且应该使用 %p 格式说明符。为什么会这样,具体有哪些问题?

int x=100;
int *pi=&x;
printf("value of pi is: %p",(void*)pi);

最佳答案

printf 是可变参数函数,必须传递正确类型的参数。标准说 %p 采用 void *

Implicit cast doesn't take place对于 variadic functions .

引自N1570 7.21.6.1 fprintf函数

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.

关于c - 跨平台使用 void 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751722/

相关文章:

java - Ghost4J/Ghostscript - 尝试将字体嵌入 PDF 时出现错误 100

C++ 引用在函数范围内创建的实例

c++ - 为什么 new 表达式可以正确生成指针类型,即使它应该返回 void*?

c - 用and int读取字符时,为什么值为4096 + ascii

使用 C 创建调试器

c - 如何从ATmega32中的软件SPI读取32位变量?

c++ - C++中没有指针的循环引用

c++ - int (*p) 和 int *p 有区别吗?

c++ - 如何使用无效*

c - C中的泛型编程