Deep understanding of the meaning of void and void pointers _c language

Source: Internet
Author: User
Tags function prototype

The meaning of void
Void is "no type" and void * is "no type pointer" and can point to any data type.

void pointer Usage specification
A ①void pointer can point to any type of data, that is, an assignment to a void pointer using a pointer to any data type. For example:
int *pint;
void *pvoid;
PVOID = pint; * * but can not pint = pvoid; */
If you want to assign pvoid to other types of pointers, you need to force type conversions such as: pint = (int *) pvoid;


② in the ANSI C standard, arithmetic operations on void pointers such as pvoid++ or pvoid+=1 are not allowed, but are permitted in GNU because, by default, the GNU considers void * as char *. sizeof (*pvoid) = = sizeof (char).

The role of Void
① The qualification returned by the function.
② The parameters of the function.
You must use void qualification when a function does not need to return a value. For example: void func (int, int);
You must use void qualification when a function does not allow parameters to be accepted. For example: int func (void).


Because a void pointer can point to any type of data, that is, you can assign a null pointer to a pointer of any data type, you may also use a void pointer as a function parameter, so that the function can accept pointers of any data type as arguments. 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 type of void and void pointer in C + + language, so there are some errors in usage. This article explains the profound implications of the Void keyword, detailing the methods and techniques used for void and void pointer types.

1. Rules carefully use void pointer type
According to the ANSI (Americannationalstandardsinstitute) standard, you cannot perform an algorithmic operation on a void pointer, that is, the following operations are illegal:
void*pvoid;
Pvoid++;//ansi: Error
Pvoid+=1;//ansi: Error
The ANSI standard is so determined because it insists that the pointer to an algorithmic operation must be determined to know its point to the data type size.
For example:
Int*pint;
Pint++;//ansi: Right
The result of pint++ is to make it larger sizeof (int).
But the famous GNU (GNU ' Snotunix abbreviation) is not so determined, it specifies void* algorithm operation and char* consistent.
The following statements are therefore correct in the GNU compiler:
Pvoid++;//gnu: Right
Pvoid+=1;//gnu: Right
The result of the pvoid++ was that it increased by 1.
In the actual program design, in order to meet the ANSI standard and improve the portability of the program, we can write code to achieve the same function:
void*pvoid;
(char*) Pvoid++;//ansi: correct; GNU: Correct
(char*) Pvoid+=1;//ansi: error; GNU: correct
There are some differences between GNU and ANSI, and the GNU is generally more "open" than ANSI, providing support for more syntax. But we should try to cater to the ANSI standard when we are actually designing.

2. Rule two if the parameter of a function can be any type of pointer, declare that its parameter is void*
A typical function prototype such as the memory manipulation function memcpy and memset is:
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 also truly reflects the meaning of the memory manipulation function because it operates on an object that is only a piece of memory, regardless of what type of memory it is. If the parameter types of memcpy and memset are not void*, but char*, then shouting is strange! Such memcpy and memset are clearly not a "pure, out of vulgar" function!
The following code executes correctly:
//Example: Memset accepts any type of pointer
int  intarray[100];
memset (intarray,0,100*sizeof (int));//Will intarray clear 0
//Example: memcpy accept any type of pointer
int  intarray1[100],  INTARRAY2[100];
memcpy (intarray1,intarray2,100*sizeof (int));//Copy Intarray2 to Intarray1
Interestingly, memcpy and memset functions return the void* type, How knowledgeable the creator of the standard library functions is!

3. Rule three void cannot represent a true variable
The following code attempts to make void represent a real variable and is therefore the wrong code:
voida;//Error
Function (Voida) //Error
Void embodies an abstraction in which the variables of the world are "of a type", such as whether a person is a man or a woman (and a transvestite?). )。
Void appears only for an abstract need, and it is easy to understand the void data type if you correctly understand the concept of "abstract base class" in object-oriented. Just as you cannot define an instance of an abstract base class, we cannot define a void (the "abstract data type") variable that allows us to analogy.

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.