In-depth understanding of C language pointers * and references & amp;

Source: Internet
Author: User

The first is an interview question about type conversion, which requires that the float variable be forcibly converted to the int type and the int & output result be given.

[Cpp] int test ()
{
Float a = 1.0f;
Cout <"value of (int) a is" <(int) a <endl;
Cout <"value of & a is" <& a <endl;
Cout <"value of (int &) a is" <(int &) a <endl;
Cout <boolalpha <(int) a = (int &) a) <endl;
Return 0;
}
Int test ()
{
Float a = 1.0f;
Cout <"value of (int) a is" <(int) a <endl;
Cout <"value of & a is" <& a <endl;
Cout <"value of (int &) a is" <(int &) a <endl;
Cout <boolalpha <(int) a = (int &) a) <endl;
Return 0;
}
In the past, forced conversion (int) variable was often used in programming. For the above example, the float type value 1.0 is used as the int type output 1, but how to understand (int &) variable, why is the output a large integer of 1065353216? The following two functions need to be distinguished: Take the address and reference. For more information, see CSDN forum.
(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 regard a as an integer (no substantial conversion is made ). Because 1 is stored as an integer and its memory data is stored as a floating point, the two are not the same. The two conversions for float B = 0.0f; are the same, but the integer form of 0 is the same as that of floating point, so in this special case, the two are equal (only in 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 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 =.

Let's look at the example of using pointer * and Reference & in variables. The reference is mainly used in the declaration of variables, that is, using this variable as an alias. The memory addresses of the two variables are identical. The following is an experiment and simple description of int * a, int & B, int * & c, int & * d.

[Cpp] int test ()
{
Int I = 10;
Int * a = & I;/* a is a pointer pointing to the address of I, & get the address */
Cout <"value of a is" <a <endl;/* I address */
Int & B = I;/* B is a reference. It points to the same address as I and can be considered as an I alias */
Cout <"value of B is" <B <endl;/* I content, 10 */
Cout <"value of & B is" <& B <endl;/* address of I */
Int * & c = a;/* c is a reference, and the reference type is Pointer (same as )*/
Cout <"value of c is" <c <endl;/* c is the alias of */
/* Int & * d = B; d is a pointer, but the reference is not an entity, so it is wrong */
Return 0;
}


From the nevasun Column

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.