Understand the meaning of void and void pointer in Depth

Source: Internet
Author: User

Void description
Void is "no type", void * is "no type Pointer", can point to any data type.

Void pointer usage Specification
① The Void pointer can point to any type of data, that is, the void pointer can be assigned a value to any type of data. For example:
Int * pint;
Void * pvoid;
Pvoid = pint;/* but cannot pint = pvoid ;*/
If you want to assign pvoid to another type pointer, You need to force type conversion, for example, pint = (int *) pvoid;

② In ansi c, arithmetic operations on Void pointers, such as pvoid ++ or pvoid + = 1, are not allowed, whereas in GNU, because by default, GNU believes that void * is the same as char. Sizeof (* pvoid) = sizeof (char ).

Role of Void
① Limitation on function return.
② Limits function parameters.
When a function does not require a return value, you must use void. Example: void func (INT, INT );
When a function does not accept parameters, you must use void. For example, int func (void ).

Because the void pointer can point to any type of data, that is, the void pointer can be assigned a value for any type of data, so the void pointer can also be used as a function parameter, in this way, the function can accept any data type pointer as a parameter. For example:
Void * memcpy (void * DEST, const void * SRC, size_t Len );
Void * memset (void * buffer, int C, size_t num );

Many beginners do not understand the void and void pointer types in C/C ++, so some errors have occurred in usage. This article will explain the profound meaning of the void keyword. The following describes how to use the void and void pointer types.

1. Exercise caution when using the void pointer type.
According to the ANSI (americannationalstandardsinstitute) standard, the void pointer cannot beAlgorithmThe following operations are invalid:
Void * pvoid;
Pvoid ++; // ANSI: Error
Pvoid + = 1; // ANSI: Error
// The ANSI standard determines this because it insists that the pointer to the algorithm operation must be determined to know the data type it points.
// Example:
Int * pint;
Pint ++; // ANSI: Correct
The result of pint ++ is to increase sizeof (INT ).
However, the well-known GNU (GNU's snotunix abbreviation) does not recognize this as it specifies that the void * algorithm operation is consistent with char.
Therefore, the following statements are correct in the GNU Compiler:
Pvoid ++; // GNU: Correct
Pvoid + = 1; // GNU: Correct
The execution result of pvoid ++ is increased by 1.
In the actualProgramIn design, to cater to ANSI standards and improve program portability, we can write and implement the same functionCode:
Void * pvoid;
(Char *) pvoid ++; // ANSI: Correct; GNU: Correct
(Char *) pvoid + = 1; // ANSI: error; GNU: Correct
There are some differences between GNU and ANSI. In general, GNU is more open than ANSI and provides support for more syntaxes. However, in actual design, we should try to cater to ANSI standards as much as possible.

2. Rule 2 If the function parameter can be of any type pointer, you should declare its parameter as void *
Typical function prototypes for memory operation functions such as memcpy and memset are:
Void * memcpy (void * DEST, constvoid * SRC, size_tlen );
Void * memset (void * buffer, intc, size_tnum );
In this way, any type of pointer can be passed into memcpy and memset, which truly reflects the significance of the memory operation function, because the object it operates on is only a piece of memory, regardless of the memory type. If the parameter types of memcpy and memset are not void * But char *, it is really strange! Such memcpy and memset are obviously not "pure, out of low-level interests" functions!
The following code is correctly executed:
// Example: memset accepts any type of pointer
Int intarray [100];
Memset (intarray, 0,100 * sizeof (INT); // clear intarray 0
// Example: memcpy accepts any type of pointer
Int intarray1 [100], intarray2 [100];
Memcpy (intarray1, intarray2, 100 * sizeof (INT); // copy intarray2 to intarray1
Interestingly, the memcpy and memset functions also return the void * type. how knowledgeable are the compilers of the standard library functions!

3. Rule 3 void cannot represent a real Variable
The following code tries to make void represent a real variable, so it is all wrong code:
Voida; // Error
Function (voida); // Error
Void represents an abstraction in which all variables in the world are "typed". For example, a person is either a man or a woman (or a demon ?).
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.

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.