C核心技术手册(三十二)
来源:互联网 发布:js object类型转时间戳 编辑:程序博客网 时间:2024/06/06 15:05
4.2.3.2指针转换为限定的对象类型
在C中,限定类型有const, volatile和restrict,例如,编译器隐式地转换任意指针为int,需要使用const int指针,如果你要删除掉限定词,就需要使用显式类型转换,如下例所描述:
int n = 77;
const int *ciPtr = 0; // A pointer to const int.
// The pointer itself is not constant!
ciPtr = &n; // Implicitly converts the address to the type
// const int *.
n = *ciPtr + 3; // OK: this has the same effect as n = n + 3;
*ciPtr *= 2; // Error: you can't change an object referenced by
// a pointer to const int.
*(int *)ciPtr *= 2; // OK: Explicitly converts the pointer into a
// pointer to a nonconstant int.
本例中倒数第二行语句描述了为什么指向const类型的指针有时叫做只读指针,尽管你可以修改指针的值,但不能修改它们所指的对象。
4.2.3.3 Null指针常量
Null指针常量为一个值为0的整型常量,或者是一个表示值0的void指针,NULL宏定义在头文件stdlib.h中,表示一个null指针常量,下例描述了使用NULL宏初始化指针。
#include <stdlib.h>
long *lPtr = NULL; // Initialize to NULL: pointer is not ready for use.
/* ... operations here may assign lPtr an object address ... */
if ( lPtr != NULL )
{
/* ... use lPtr only if it has been changed from NULL ... */
}
当你将null常量指针转换为另一个指针类型,结果称为null指针,null指针的位模式不需要一定为0,然而,当你比较null指针和0,或NULL、或另一个null指针,结果通常为true,null指针与指向有效类型或函数的指针比较时,通常结果为false。
- C核心技术手册(三十二)
- C核心技术手册(一)
- C核心技术手册(二)
- C核心技术手册(三)
- C核心技术手册(四)
- C核心技术手册(五)
- C核心技术手册(六)
- C核心技术手册(七)
- C核心技术手册(八)
- C核心技术手册(九)
- C核心技术手册(十)
- C核心技术手册(十一)
- C核心技术手册(十二)
- C核心技术手册(十三)
- C核心技术手册(十四)
- C核心技术手册(十五)
- C核心技术手册(十六)
- C核心技术手册(十七)
- 最大间隙问题
- python版本导致print出错z
- 关于锁的一些想法
- sicily 1341
- Excel的奇门绝技
- C核心技术手册(三十二)
- Quick Launch(win7)
- Excel中避免计算结果出错的技巧
- C#核心概念--装箱和拆箱(什么是装箱和拆箱)
- C核心技术手册(三十三)
- sicily 1926
- 基于JMX,用MC4j远程监控 Tomcat
- JavaMelody
- 关于Hibernate的getCurrentSession()!