What happens to type conversions, pointer type conversions, and C + + coercion

Source: Internet
Author: User

(1) Forced type conversion

Forcing type conversions is the process of re-interpreting 0 and 1 of the memory corresponding to the variable according to the new type

Code:

#include <iostream>using namespace Std;int main (int, char *[]) {char c = 0xa0;//0xa0 [ -128~127] The highest bit is 1 for negative numbers  0xA0 = 1010 0000 = -96//High-fill 0 0xa0->0xff ff ff A0 = 0000 0000 0000 0000 0000 0000 1010 0000 = 160printf ("c =%d\n", c);//high-fill 1 0x A0->0XFF ff ff A0 = 1111 1111 1111 1111 1111 1111 1010 0000 = 4294967200cout<< "c =" << (unsigned int) C<&L T;ENDL;//0XA0, the cast takes place on the left side of the assignment symbol, where the 0 and 1 in memory are re-interpreted according to the type of the cast,//That the memory contains 0 and 1 representing a unsigned char,unsigned char UC = ( unsigned char) c;//[0~256] 0xA0 = 1010 0000 = 2^7+2^5 = 128+32 = 160cout<< "UC =" << (unsigned int) Uc<<endl ;//0xa0 = 0x00a0 = = (160) 10 binary}

Description picture and run result





(2) pointer type conversion


The value of the pointer variable after the conversion (the address of the variable) does not change when the pointer reads and writes the address after the conversion:

A, the length of the read variable (in bytes) is read according to the converted variable size (this may be accessed out of bounds!) )

b, the read content is re-interpreted according to the converted variable type, same as (1)


What happens to type conversions, pointer type conversions, and C + + coercion

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.