Pointer to void

Source: Internet
Author: User

A pointer pointing to any object type can be assigned to a variable of Type void *, void * can be assigned to another void *, and two voids * can be equal or not, in addition, you can explicitly convert void * to another type. Other operations are not safe, because the compiler does not know which object is actually referred. Therefore, any other operations on Void * will cause compilation errors. To use void *, you must explicitly convert to a pointer to a specific type.

Example:

Void test (int * PI)

{

Void * Pv = PI; // OK, implicit conversion from int * to void *

* PV; // error: void * cannot be referenced indirectly

PV ++; // error: void * cannot be incremental (the size of the specified type is unknown)

Int * PI1 = static_cast <int *> (PV); // OK: explicitly converted back to int *

Double * Pi2 = PV; // Error

Double * PI3 = PI; // Error

Double * PI4 = static_cast <double *> (PV); // OK: insecure

}

1. In general, if a pointer is converted ("forced", cast) to a pointer different from the actual type of the object to be approved, using the last pointer is insecure. The most important purpose of void * is to pass a pointer to the function goods, and the object type cannot be assumed. In addition, a non-type object is returned from the function. Use such an object. You must use explicit type conversion.

2. the pointer to the function and the pointer to the member cannot be assigned to void *

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.