C ++ universal pointer-void

Source: Internet
Author: User

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;
}

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.