A profound analysis of the meaning of void and void pointers

Source: Internet
Author: User

The meaning of

void

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

void specification for use of pointers
The ①void pointer can point to any type of data, that is, the null pointer is assigned to a pointer of 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 Ansic 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).

void the role
① 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);

------------------------------------------------------------------------------


1. Overview

Many beginners do not understand the type of void and void pointer in C + + language, so there are some errors in usage. This article will explain the profound meaning of the void keyword, and detail the methods and techniques for the use of void and void pointer types.

the meaning of 2.void    voidthe literal meaning is "no type",void*is "No type pointer",void*can point to any type of data. voidalmost only "annotation" and limit the role of the program, because no one would ever define avoidvariables, let's try to define:
void A;
This line of statements is compiled with an error, prompting "illegal use of type ' void '". However, even if Voida compiles without errors, it has no real meaning.
Void really plays a role in:
(1) The qualification of function return;
(2) The qualification of function parameters.
As we all know, if pointers P1 and p2 are of the same type, then we can assign values to each other directly between P1 and P2, and if P1 and P2 point to different data types, you must use the coercion type conversion operator to convert the pointer type to the right of the assignment operator to the type of the left pointer.
For example:
float * P1;
In t* p2;
P1 = p2;
where the P1 = P2 statement compiles an error, prompting "' = ': cannotconvertfrom ' int * ' to ' float * ', which must be changed to:
p1= (float*) P2;
Unlike void*, any type of pointer can be assigned directly to it without coercion of type conversions:
void * P1;
int * P2;
P1 = p2;
This does not mean, however, that void* can also be assigned to other types of pointers without coercion of type conversions. Because "no type" can contain "have type", and "have type" cannot contain "no type". The reason is very simple, we can say "men and women are human", but can not say "man is a Man" or "man is a woman." The following statement compiles an error:
void * P1;
int * P2;
P2 = p1;


Prompt "' = ': Cannotconvertfrom ' void* ' to ' int* '". The use rule for the Void keyword is given under

3.void use :
Rule One if the function does not return a value, declare it as a void type
in the C language, a function that is not qualified with the return value type, is processed by the compiler as the return integer value. But many programmers mistakenly assume that they are of type void. For example:
Add (INTA,INTB)
{
return a+b;
}
int main (int argc,char * argv[])
{
printf (/"2+3=%d/", add (2,3));
}
The result of the program running is output:
2+3=5
This shows that a function that does not have a return value is indeed an int function. "The C + + language has a very strict type security check that does not allow this to happen," says Dr. Lin Rui, a high quality C + + programming program in
. However, the compiler does not necessarily think so, for example, in the visualc++6.0 of the Add function compiled without error and no warning and run correctly, so you can not hope that the compiler will do a rigorous type check.
Therefore, in order to avoid confusion, we must specify the type of any function, without omitting it, when writing a C + + program. If the function does not return a value, be sure to declare it as a void type. This is not only the need for good readability of the program, but also the requirements of programming standardization. In addition, with the void type declaration, you can also play the "Self annotation" function of the code. The code's "self annotation" means that the code can annotate itself.   [Page]
Rule Two If a function has no arguments, declare its argument to void
Declare one such function in the C + + language:
int function (void)
{
return1;
}   
It is not legal to make the following call:
function (2);
Because in C + +, the function argument is void

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.