C ++ void pointer and NULL pointer

Source: Internet
Author: User

The pointer has two attributes: the address and length pointing to the variable/object, but the pointer only stores the address. The length depends on the pointer type.
The compiler varies the addressing range according to the pointer type from the pointer to the address to the backward addressing pointer type,
For example:
Int * searches for 4 bytes from the specified address as the storage unit of the variable.
Double * searches for 8 bytes from the specified address as the storage unit of the variable.

Void has almost only the function of "commenting" and limiting programs.

 

Void actually plays the following role:
(1) Restrictions on function return;
(2) Restrictions on function parameters

 

The following rules use void:
Rule 1 if the function does not return a value, the void type should be declared.
Rule 2 If the function has no parameters, the parameter should be declared as void
Rule 3 use the void pointer with caution
According to the ANSI (American National Standards Institute) standard, you cannot perform algorithm operations on the void pointer, that is, the following operations are invalid:
The ANSI standard determines this because it insists that the pointer to algorithm operations must be determined to know the data type to which it points.
Void * pvoid;
Pvoid ++; // ANSI: Error
Pvoid + = 1; // ANSI: Error
Int * pint;
Pint ++; // ANSI: Correct
 Rule 4 if the function parameter can be a pointer of any type, the parameter should be declared as void *

Typical function prototypes for memory operation functions such as memcpy and memset are:
Void * memcpy (void * DEST, const void * SRC, size_t Len );
Void * memset (void * buffer, int C, size_t num );
Any type of pointer can be passed into memcpy and memset, which also truly reflects the significance of the memory operation function, because it operates on only one piece of memory, regardless of the memory type. If the parameter types of memcpy and memset are not void *,

Rule 5 void cannot represent a real Variable
The emergence of void is only for an abstract need. If you have understood the concept of "abstract base class" in object-oriented, it is easy to understand the void data type. Just as we cannot define an instance for an abstract base class, we cannot define a void (let's say that void is an abstract data type) variable.

 

 

The following explains a concept. Although void pointer and NULL pointer are both translated as null pointers in Chinese, they are different.

Null Pointer:
Null Pointer does not point to any actual physical memory address
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.

The NULL pointer differs from the uninitialized pointer: (The following is an example)
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. h>
Int * exforsys;
Void main ()
{
* Exforsys = 100;
}

The output of the above program is
NULL pointer assignment
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.