Analysis of C ++ pointers, arrays, and references

Source: Internet
Author: User
Tags value of pi

I. pointer: The content is a variable indicating a memory address. The type indicates how the compiler interprets the pointer content pointing to the content in the address and the size of the memory area. Example: [cpp] int I = 0; int * pi = & I; printf ("pi = % x \ n", pi); // print the pi content: 0x2000 printf ("* pi = % d \ n", * pi); // print the value of pi pointing to the address: 5 printf ("& pi = % x \ n", & pi); // print the pi address: 0x100 from the assembly point of view, the pointer is like this: int I = 0; 010E139E mov dword ptr [I], 0 int * pi = & I; 010E13A5 lea eax, [I] 010E13A8 mov dword ptr [pi], eax 2. array: a collection of single data type objects. A single object is not named and accessed through an index. The difference between array name and pointer: array name indicates that an object is a data structure, which is an array. The extension of an array name is that it can be converted to a pointer pointing to the object and is a pointer constant. The pointer to an array is another variable type, which only means the address of the array. Note: although the array name can be converted to a pointer to the object, however, it can only be regarded as a pointer constant and cannot be modified, as follows: [cpp] int intArray [10]; intArray ++; // What does the error "equivalent pointer and array" mean? The index operation is the same, for example, p [2]; a [2]; 3. reference is the alias of an object. After an object is initialized and referenced, the object name and reference point to the object. How is the reference implemented? From the perspective of assembly language, pointers and references are the same: [cpp] int I = 0; 00E9139E mov dword ptr [I], 0 int & ref = I; 00E913A5 lea eax, [I] 00E913A8 mov dword ptr [ref], eax int * pi = & I; 00E913AB lea eax, [I] 00E913AE mov dword ptr [pi], difference between eax pointer and reference (from the perspective of C ++ usage): There is no null Reference reference. It is guaranteed by the compilation phase that the reference cannot point to another object after initialization. Note: A Reference pointing to a constant cannot be initialized with a literal or temporary value, but a reference pointing to a constant is acceptable. Example: [cpp] double & d = 12.3; // error! Const double & d = 12.3;

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.