在不同的平台空指针的使用 [英] Usage of void pointers across different platforms

查看:102
本文介绍了在不同的平台空指针的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说指针首先应转换为void,以确保在不同的平台价值的一致性,应使用%P 格式说明。为什么它,到底是什么问题?

  INT X = 100;
INT * PI =安培; X;
的printf(圆周率的值是:%P(无效*)PI);


解决方案

的printf 是一个可变参数函数,必须通过正确类型的参数。该标准说%P 需要无效*

<一个href=\"http://stackoverflow.com/questions/26751361/understanding-implicit-casts-in-c/26751407#26751407\">Implicit投不发生了解可变参数的函数的。

通过 N1570 7.21.6.1的引用fprintf中功能


  

号码:参数应是无效的指针。指针的值是
  转换为打印字符的序列,在一个定义的实现
  方式。


I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems?

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

解决方案

printf is a variadic function and must be passed arguments of the right types. The standard says %p takes void *.

Implicit cast doesn't take place for variadic functions.

Quoting from N1570 7.21.6.1 The fprintf function

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.

这篇关于在不同的平台空指针的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆