(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