The literal value of void is "untyped" and void* is "untyped pointer". Void* can point to any type of data. Void is almost only "annotated" and restricts the role of the program, since no one will ever define a void variable.
void A; Compile-time prompt "Illegaluseoftype ' void '"
Void really plays a role in: the qualification of function returns, and the qualification of function parameters
If the pointer P1 and p2 are of the same type, then P1 and P2 can be assigned to each other, 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 before you can assign a value. Such as:
float* P1;
int* P2;
P1 = p2; Can ' t convert from ' int* ' to ' float* ', must be changed to the following form
p1= (float*) P2;
Void* is different, any type of pointer can be directly assigned to it, without forcing the type conversion, for example: void* p1;int* p2;p1 = p2;
This does not mean that Feng void* can also assign to other types of pointers without casting, because "no type" can contain "typed" and "There is no type" for "untyped"
void keyword usage rules
1. If the function does not return a value, it should be declared as void type. In C, where a function without a return value type qualifies, the compiler is treated as a return integer value, but many programmers mistakenly consider it to be of type void
For example
Add (int a,int b) int main ()
{ {
return a+b; printf ("2+3=%d", add (2,3));
}}//program run result is 2+3=5, which means no return value description of the function, its return value is indeed an int type
Therefore, to avoid confusion, you must specify its return type for any function. If the function does not return a value, be sure to declare it as void type. Play the self-explanatory role of code. This is both the need for good readability of the program and the requirements of programming norms.
2. If the function has no arguments, declare its argument void. If you declare one of these functions in the C + + language
the int function (void)//is not valid for function (2), because in C + +, this function parameter is void, meaning that it does not accept any arguments, but if compiled in turbo C2.0 is correct
{//description in the C language, you can pass arguments of any type to a function without parameters, but compiling the same code in the C + + compiler will make an error. Therefore, if the function does not accept any arguments, it must be declared void
return 1;
}
3. If the parameter of a function can be any type of pointer, declare its argument as void*
Typical functions such as memory manipulation:
void* memcpy (void* dest,const void* src,size_t len);
void* memset (void* buffer,int c,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 it operates on only one piece of memory, regardless of what type of memory it is.
4.void cannot represent a real variable.
void A//error
Void embodies an abstraction in which the variables in the world are "typed". 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".
Two. sizeof
Deep understanding of void,void* and sizeof keywords