Pointer type casting

Source: Internet
Author: User
Tags type casting

The problem is the following:

#include <iostream.h>
void Main () {
int k=1;
int *f=&k;
* (float*) (f) = 1;
cout<<*f;
}//output why not 1?


At this point, we are able to see the formatted transformations in the function. Assuming that the format conversion exists in addition to void format, there are two and more than two of the format of the conversion, then the pointer refers to the memory of the variable will change, resulting in a corresponding change in format


For the subject:


When you force an int pointer into a float pointer, the data actually goes beyond the range of memory space defined by int


For example int occupies 4 bytes float occupies 8 bytes start k=1 stored in 4 bytes
When you convert the pointer to float, he reads 1 of the four bytes and the extra four bytes after it so it's definitely not 1, as for what would be random.

Pointer type casting

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.