the profound meaning of the C-language void keyword1. Overview This article explains the deep meaning of the void keyword and details the use and techniques of void and void pointer types. 2The meaning of void is literally "untyped",void* is "no type pointer",void*can point to any type of data. Void is almost only "annotated" and restricts the function of the program, since no one will ever define a void variable, let's try to define:voidA; This line of statements compiles with an error, prompting "illegal use of type'void'". However, even if the compilation of void A does not go wrong, it does not have any practical significance. Void really plays a role in: (1) The qualification of the function return;2) The qualification of the function parameters. We will specify the above two points in the third section. It is well known that if pointers P1 and p2 are of the same type, then we can assign values directly between P1 and P2, and if P1 and P2 point to different data types, you must use the force 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;int*P2;P1=P2; where P1= P2 statement compiles an error, prompting "'=': Cannot convert from 'int *'To'Float *'", must instead: P1= (float*) P2; and void*is different, any type of pointer can be assigned directly to it without forcing the type conversion:void*P1;int*P2;P1=P2; But that doesn't mean thatvoid*You can also assign type conversions to other types of pointers without forcing them. Because "untyped" can contain "there is a type," and "there is a type" cannot tolerate "untyped". The truth is simple, we can say "men and women are people", 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; Tip "'=': Cannot convert from 'void *'To'int *'". 3use of the Void keyword is given below: rule one if the function does not return a value, it should be declared as void type in C, where a function that does not return a value type qualifies, it is processed by the compiler as a return integer value. But many programmers mistakenly think of it as void type. For example: Add (intAintb) {returnA +b;}intMainintargcChar*argv[]) {printf ('2 + 3 =%d', Add (2,3) );} The result of the program running is output:2+3=5This means that a function that does not add a return value description is indeed an int function. Dr. Lin Rui, "high-quality C/c++ programming Says: "The C + + language has very strict type security checks and does not allow this to happen (the function does not add type declarations)." However, the compiler does not have to be so determined, for example, in Visual C + +6the compilation of the above add function in. 0 is error-free and has no warning and is running correctly, so you cannot expect the compiler to do strict type checking. Therefore, in order to avoid confusion, we are writing C/c++program, you must specify a type for any function without leaking it. If the function does not return a value, be sure to declare it as void type. This is both the need for good readability of the program and the requirements of programming norms. In addition, the "self-commenting" function of the code can also be played after the void type declaration is added. The code's "self-explanatory" code can annotate itself. Rule two if the function has no arguments, declare that its argument is void in C++the language declares a function like this:intfunctionvoid){return 1;} It is not legal to make the following call: function (2); Because in C++, the function parameter is void meaning that the function does not accept any arguments. We're in Turbo C .2. 0 Compilation: #i nclude'stdio.h'Fun () {return 1;} Main () {printf ('%d', Fun (2) ); GetChar ();} Compiles correctly and outputs 1, which means that in C, you can pass parameters of any type to a function without parameters, but in CCompiling the same code in the + + compiler will make an error. In C + +, you cannot pass any parameters to a function without parameters, and an error prompt "' Fun': function does not take1parameters ". So, whether it's C or C,++, if the function does not accept any arguments, be sure to indicate that the argument is void. Rule three caution using the void pointer type in accordance with the ANSI (American National standards Institute) standard, you cannot perform algorithmic operations on void pointers, that is, the following operations are illegal:void*pvoid;pvoid++;//ANSI: ErrorPVOID + =1;//ANSI: Error//The ANSI standard is determined because it insists that the pointer to the algorithm operation must be determined to be aware of its point to the data type size. //For example:int*Pint;pint++;//ANSI: CorrectThe result of the pint++ is that it increases sizeof (int). But the famous GNU (GNU'The abbreviation for S not Unix) is not so determined, it specifies that the algorithm operation of Void * is consistent with char *. The following statements are therefore correct in the GNU compiler: Pvoid++;//GNU: CorrectPVOID + =1;//GNU: Correctpvoid++The result of the implementation is 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 that implements the same function:void*pvoid; (Char*) pvoid++;//ANSI: Correct; GNU: Correct(Char*) Pvoid + =1;//ANSI: Error; GNU: CorrectThere are some differences between GNU and ANSI, and in general, the GNU is more "open" than ANSI, providing support for more grammars. But we should be able to meet the ANSI standards as much as possible when it comes to real design. Rule four if the argument to a function can be any type of pointer, declare that its argument is void*typical function prototypes, such as memory manipulation functions memcpy and memset, are:void* MEMCPY (void*dest,Const void*src, size_t len);void* Memset (void* Buffer,intc, size_t num); Thus, any type of pointer can be passed into memcpy and memset, which also truly embodies the meaning of the memory manipulation function, because the object it operates is only a piece of memory, regardless of what type of memory it is. If the parameter type of memcpy and memset is not void*, but char *, that's shouting truth strange! Such memcpy and memset are obviously not a "pure, out of the vulgar" Function! The following code executes correctly://Example: Memset accepts arbitrary type pointersintintarray[ -];memset (Intarray,0, -*sizeof(int) );//the intarray will be cleared 0//Example: memcpy accepts arbitrary type pointersintintarray1[ -], intarray2[ -];memcpy (Intarray1, Intarray2, -*sizeof(int) );//Copy the intarray2 to Intarray1Interestingly, the memcpy and memset functions also return void *type, the writer of standard library functions is so knowledgeable! Rule five void cannot represent a real variable the following code attempts to have void represent a real variable and therefore is the wrong code:voidA//Errorfunctionvoida);//ErrorVoid embodies an abstraction in which variables in the world are "typed", such as a man who is not a man or a woman. Void appears only for an abstract need, and it is easy to understand the void data type if you correctly understand the concept of an object-oriented "abstract base class". Just as it is not possible to define an instance of an abstract base class, we cannot define a void (the "abstract data type") variable that allows us to compare.
The profound meaning of the C-language void keyword