[C ++ primer] 4.10 exercise 12 questions (INT), (Int &), and (int *)

Source: Internet
Author: User
(1) first, let's look at the differences between (INT) and (Int &) through an example:

Float a = 1.0f;
Cout <(INT) A <Endl;
Cout <(Int &) A <Endl;
Cout <boolalpha <(INT) A = (Int &) a) <Endl; // boolalpha represents true and false in symbolic form
Float B = 0.0f;
Cout <(INT) B <Endl;
Cout <(Int &) B <Endl;
Cout <boolalpha <(INT) B = (Int &) B) <Endl;

 

Output:

1

1065353216

False

0

0

True

 

Explanation:

(INT) A actually constructs an integer with the floating point A as the parameter. The value of this integer is 1;

(Int &) A tells the compiler to treat a as an integer (there is no substantial conversion)

Because 1 is stored as an integer and its memory data is stored as a floating point, the two are not the same.

However, the integer form of 0 is the same as that of floating point, so in this special case, the two are equal (only in the numerical sense ).


Note:

The output of the program will show (Int &) A = 1065353216. How does this value come from? As mentioned above, 1 is stored in the memory as a floating point number. According to ieee754, the content of 1 is 0x0000803f (Bytes reverse order has been considered ). This is the value of the memory unit occupied by the variable. When (Int &) A appears, it is equivalent to telling its context: "treat the content in this address as an integer! Don't worry about what it was ." In this way, the content 0x0000803f is interpreted as an integer, and its value is exactly 1065353216 (decimal number ).
By viewing the assembly code, we can confirm that "(INT) A is equivalent to re-constructing an integer number with a value equal to a", while (Int &) the function is only to express a type information, meaning that the correct Heavy Load version is selected for cout <and =.

 

(2) (int *) is to display a variable (this variable cannot be a floating point type, it can be an integer, character, pointer) in the form of an address, and (Int &) the principle is the same. When (Int & *) A appears, it is equivalent to telling its context: "treat the content in this address as an address! Don't worry about what it was ."

For example:

Char P = 'a ';

Cout <(INT) P <Endl;

Cout <(int *) P <Endl;

 

Output:

97

00000061 // The hexadecimal number of 97

 

This is useful when outputting the first address of the character array:

Char * P = "ABCD ";

Cout <(int *) P <Endl;

 

Otherwise, cout <p <Endl;

The output is ABCD rather than the first address.

Related Article

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.