Void*and Null Pointer的区别
来源:互联网 发布:淘宝联想授权网店查询 编辑:程序博客网 时间:2024/05/17 04:01
In this C++ tutorial , you will learn about two interesting types of pointers; void pointers and Null Pointer. These pointers will be discussed in conjunction with syntax, usage and example.
Pointer to Void
General Syntax:
void* pointer_variable;
Void is used as a keyword.
Referring back to pointer definitions and usage, it is known that the data
For example:
- int i;
- float f;
- int* exf;
- float* test;
- then
- exf=&i;
Is correct because the address of integer variable is stored in an integer pointer.
If a user writes the statement:
exf=&f;
Then this statement produces an error. The address of the float variable is stored in an integer pointer that is incorrect.
Similarly, if the programmer tries to place the address of an integer variable to a float pointer, such as:
test=&i;
The above statement will also show an error.
The Pointer to Void is a special type of pointer that the programmer can use to point to any data type.
Using the above example, the programmer declares pointer to void in this manner:
void* sample;
Using the above example's definition and assigning the pointer to void to the address of an integer variable is perfectly correct.
sample=&i;
Using the above example to define the pointer to void and assign the pointer to void to the address of a float variable as below is also perfectly correct.
sample=&f;
Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).
NULL Pointer:
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.
For example:
int* exforsys;
exforsys=0;
The above statement denotes exforsys as an integer pointer type that does not point to a valid memory address. This shows that exforsys has a NULL pointer value.
The difference between void pointers and NULL pointers:
A Void pointer is a special type of pointer of void and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.
For example, if a programmer uses the program below:
- #include <iostream>
- using namespace std;
- int *exforsys=NULL;
- void main()
- {
- *exforsys=100;
- }
The output of the above program is NULL POINTER ASSIGNMENT, which will result in:
The above program will result in a runtime error. This means that the pointer variable exforsys is not assigned any valid address and, therefore, attempting to access the address 0 gives the above error message.
原文链接:http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-void-pointer-and-null-pointer.html
- Void*and Null Pointer的区别
- NUll and 0 Pointer
- Wild pointer, NULL pointer, generic pointer and dangling pointer
- void ,NULL与0的区别联系
- void ,NULL及0的区别联系
- Null Pointer and Cast Exception
- void pointer
- 引用和指针的区别 Difference between pointer and reference
- Null, Empty, Nothing, and vbNullString的区别
- null pointer
- void 和 void* 的区别
- void ( *p ) (void) 和 void ( *p () ) (void)的区别
- 空悬指针和野指针的区别(the difference between dangling pointer and wild pointer)
- Null pointer (NULL array pointer is passed) in function cvGetMat, 报这样的错
- 解释void *与void **的区别
- + (void)load 和+ (void)initialize的区别
- 关于void*与void**的区别
- + (void)load 和+ (void)initialize的区别
- 指尖下的js ——多触式web前端开发之一:对于Touch的处理
- 设为首页代码和加入收藏代码-兼容各种浏览器
- Linux下nm和ldd 命令
- Hibernate对象的三种状态
- TWaver图形之道(七)TWaver开发流程
- Void*and Null Pointer的区别
- iphone开源项目list
- python dict list
- Android用户输入系统结构和移植内容
- 远程协助的伪原创工具
- UIButton 按钮控件-IOS开发
- 使用[NSMutableDictionary dictionary]程序崩溃问题(野指针)
- VC6.0 改变按钮颜色及文字颜色(2)
- 指尖下的js ——多触式web前端开发之二:处理简单手势