6.1 The concept of pointers?

Source: Internet
Author: User

If a variable is defined in a program, the variable is assigned an internal deposit element at compile time. The system allocates a certain length space according to the variable type defined in the program. VC C + + 6.0, an integer variable is assigned 4 bytes, a micro-precision floating-point variable is assigned 4 bytes, and a character variable is assigned 1 bytes. each byte in the memory area has a number, and this is the "address", which corresponds to the room number in the hotel. the data is stored in the memory unit identified by the address, which is the same as the passenger residing in each room in the hotel.

What is the difference between the address of a memory cell and the contents of a memory unit?

Assume that the program defines 3 integer variables I, j,k, compile-time system allocation 2000,2001,2002,2003 4 bytes to the variable i;

Assign 2004,2005,2006 and 2007 bytes to J; Allocate 2008,2009,2010,1011 bytes to K;

In the program is generally through the variable name to the internal storage element for access operations.

In fact, after compiling the variable name into the address of the variable, the variable is worth accessing is through the address.

For example: the statement "cout<<i;" is executed as such:

Based on the corresponding relationship between the variable name and address (the corresponding relationship is determined at compile time), find the address of the variable I 2000, and then remove the data from the 4 bytes starting from 2000 (that is, the value of the variable 3), output him. At the time of input, if "cin>>i;" is used, the value entered from the keyboard will be sent to an integer storage unit starting at address 2000 at the time of execution.

If there is a statement "K=I+J;", the value of I is removed from an integer variable storage unit starting at 2000 bytes (the value in the figure is 3), and the value of J is removed from the variable storage cell starting at 2004 bytes (the value is 6). Add them to the integer storage unit that starts with (9) to the 2008 bytes occupied by K. This variable address access variable is worth the way called Direct access, or direct access mode.

Of course, you can use another way of indirectly accessing (indirect access), the address of the variable i is stored in another variable. You can define such a special variable in your program, which is used exclusively for storing addresses. Suppose the first variable is i_pointer, which is used to store an integer variable address. The compilation system assigns the variable 4 bytes (assuming 3010 to 3013) 3010,3011,3012,3013.

i_pointer=&i; After executing this sentence, the value of I_pointer is 2000 (that is, the starting address of the unit that the variable I occupies). To take the value of the variable I, in addition to the direct way, you can also use the indirect way: First find the "I address" of the variable I_pointer, remove the address of I (that is, 2000), and then to 2000 start of the 4 bytes to remove the value of I (3).

Let's make an analogy. In order to open a drawer, there are 2 ways, one is to bring a key to the body, when needed to directly find the key to open A drawer, remove the required items. Another way: For the sake of safety, put the a key in another drawer B lock up. If you need to open a drawer, you need to find the B drawer, open the B drawer, remove the A key, open a drawer, take out a drawer of things, this is indirect access .

6.1 The concept of pointers?

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.