我听说应该首先将指针转换为 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/