Void pointer conversion and void pointer Conversion

Source: Internet
Author: User

Void pointer conversion and void pointer Conversion
Pointers of any type can be explicitly converted to void without data loss. For example:


# Include <stdio. h>

Int main (void)
{
Short a = 5;
Void * p1;
Short * p2;
P1 = (void *) &;
P2 = (short *) p1;
Printf ("% d \ n", * p2 );
Return 0;
}


The address of a is 0x0012ff7c. Therefore, the address 0x0012ff7c is stored in p1, and its data is 5, but 5 cannot be accessed through p1. If you want to access data, you can use Explicit conversions to convert p1 to the short type (data 5 itself is the short type), that is, p2. In this case, you can call p2 to access data 5 without data loss.


In the above case, the pointer format of the same length is converted to each other, and its size remains unchanged, and the data will not be distorted.


If a void pointer is forcibly converted to int and then to long, the size will change and the data will also be distorted.


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.