This article provides a deep description of the void keyword and describes how to use the void and void pointer types. Beginners do not understand the void and void pointer types in C/C ++, so there are some errors...
1. Meaning of void
Void literally means "no type", void type * is "no type Pointer", void * can point to any type of data. Void has almost only "Comments" and restrictions on the role of the program, because no one will ever define a void variable, let's try to define:
An error occurs when compiling this line of statements, and the message "illegal use of type 'void'" is displayed '". However, even if the compilation of void a does not go wrong, it has no practical significance. Void actually plays the following role:
1) limits on function return;
2) limits function parameters.
As we all know, if the pointer p1 and p2 are of the same type, we can directly assign values to each other between p1 and p2. If p1 and p2 point to different data types, you must use the forced type conversion operator to convert the pointer type on the right of the value assignment operator to the pointer type on the left.
- Int function (void)
- {
- Return 1;
- }
- The following call is invalid:
- Function (2 );
- In C ++, the function parameter void means that this function does not accept any parameters.
- We compile in Turbo C 2.0:
- # Include "stdio. h"
- Fun ()
- {
- Return 1;
- }
- Main ()
- {
- Printf ("% d", fun (2 ));
- Getchar ();
- }
Therefore, in order to avoid confusion, when writing C/C ++ programs, you must specify the type of any function. If the function does not return a value, it must be declared as void. This is both a need for good program readability and a requirement for standardized programming.
In addition, after adding the void type declaration, you can also use the "self-annotation" function of the Code. The "self-annotation" of the code means that the code can annotate itself. Rule 2 If the function has no parameters, the parameter should be declared as void
Declare a function in C ++:
- Int function (void)
- {
- Return 1;
- }
- The following call is invalid:
- Function (2 );
- In C ++, the function parameter void means that this function does not accept any parameters.
- We compile in Turbo C 2.0:
- # Include "stdio. h"
- Fun ()
- {
- Return 1;
- }
- Main ()
- {
- Printf ("% d", fun (2 ));
- Getchar ();
- }
Compiling is correct and 1 is output. In C, parameters of any type can be transferred to a function without parameters. However, compiling the same code in C ++ compiler will cause an error. In C ++, no parameters can be transferred to a function without parameters. If an error occurs, the system prompts "'fun ': function does not take 1 parameters ". Therefore, whether in C or C ++, if the function does not accept any parameters, you must specify the parameter as void.
- How to Write C ++ project development and project plan correctly
- Summary Notes on learning and exploring C ++ library functions
- In-depth demonstration of high security of C ++
- Describes in detail how to accurately Write C ++ languages.
- In-depth demonstration of high security of C ++