Void pointer
The pointer has two attributes: the address and length of the variable/object.
However, pointers only store addresses. The length depends on the pointer type.
The compiler uses the pointer type to backward address from the address pointed to by the pointer.
If the pointer type is different, the addressing range is also different, for example:
Int * searches for 4 bytes from the specified address as the storage unit of the variable.
Double * searches for 8 bytes from the specified address as the storage unit of the variable.
1. Void pointer is a special pointer
Void * VP
// It is especially because it has no type
// Or this type cannot determine the object length.
2. Any pointer can be assigned to the void pointer.
Type * P;
Vp = P;
// Conversion not required
// Only get the variable/object address without obtaining the size
3. The Void pointer must be converted when assigned to other types of pointers.
Type * P = (type *) VP;
// The conversion type is to get the variable/object size.
Turn: http://icoding.spaces.live.com/blog/cns! 209684e38d520ba6! 130. Entry
4. The Void pointer cannot be referenced again.
* VP // Error
Because the void pointer only knows, pointing to the starting address of the variable/Object
I do not know the size of the variable/object (several bytes), so I cannot correctly reference it.
5. The Void pointer cannot be used in Pointer operations unless it is converted.
(Type *) VP ++;
// Vp = VP + sizeof (type)
# Include <iostream>
# Include <stdlib. h>
# Include <string>
Using namespace STD;
Typedef struct tag_st
{
Char ID [10];
Float Fa [2];
} St;
// I used this in the program
Int main ()
{
St * P = (St *) malloc (sizeof (ST ));
Strcpy (p-> ID, "Hello! ");
P-> Fa [0] = 1.1;
P-> Fa [1] = 2.1;
St * q = (St *) malloc (sizeof (ST ));
Strcpy (Q-> ID, "World! ");
Q-> Fa [0] = 3.1;
Q-& gt; Fa [1] = 4.1;
Void ** plink = (void **) P;
* (St *) (plink) = * q; // plink needs to be forcibly converted first to make it know the size to be overwritten.
// The P content overwrites the Q content.
Cout <p-> id <"" <p-> Fa [0] <"" <p-> Fa [1] <Endl;
Return 0;
}