C (5), C-5 transport

Source: Internet
Author: User

C (5), C-5 transport

Pointer

This section introduces:

In the previous section, we analyzed the arrays in the C-language Composite data type. In this section, we will

Key point: the soul of C language-pointer learning! Advantages of using pointers: Using pointers can represent and use complex data structures;

It makes it easier to use our arrays and strings. It can directly process memory unit addresses like assembly languages. It can dynamically implement memory space.

Allocation, the C language pointer is the focus, and it is also the difficulty in C, let everyone follow the footsteps of the author, to analyze the pointer in C!


This section describes the road map:





Pointer concept, pointer and one-dimensional array and string:




Pointer and two-dimensional array:


This knowledge point is a pointer difficulty. To understand this point, we only need to place the center of gravity on the two-dimensional array to thoroughly understand the two-dimensional array!

Step 1: a two-dimensional array stores data by rows and columns. We can consider it as a one-dimensional array consisting of several rows and several columns in each row.

Int a [3] [4] = {1, 2, 3, 4}, {1, 2, 3}, {1, 3, 3}; it can be viewed as three rows, each row has four one-dimensional arrays.


Step 2: a represents the address of the first element of the Two-dimensional array, that is, & a [0]. If you wrap the second and third rows of the array, you only need to: a + 1, a + 2; equal to & a [1], & a [2]


Step 3: we can regard a [0] as a pointer to a [0] [0]. Then a [0] + 1 points to a [0] [1], that is, the address is & a [0] [1].

Ps: here, the 1 Value of + 1 is the number of bytes occupied by array elements. For example, if int a [], this + 1 indicates moving four bytes backward, points to the next element;

If it is + 2, it is to move 8 bytes behind, so a [I] [2] = a [I] + 2



Step 4: by studying the previous one-dimensional array, we can know that a [I] is equivalent to * (a + I ).

Similarly, when we extend to a two-dimensional array, a [I] [j] is equivalent to * (a + I) + j and a [I] + j, indicates the address of a [I] [j ].

Evaluate the value of a [I] [j]: * (a [I] + j) and * (a + I) + j ).


Note: * a and * (a + I) do not represent any specific array elements. Therefore, errors may occur when you reference them!


① Array first address a [0]: a cannot be referenced directly !!!

② Address of the first dollar of one-dimensional array a [0] (& a [0] [0]): a [0], * (a + 0), *;

③ The first address of the first line: a + 1 cannot be directly applied.

④ Address of the first element of one-dimensional array a [1] (& a [1] [1]): a [1], * (a + 1)

⑤ Address of element a [2] [4] in two rows and four columns: a [2] + 4, * (a + 2) + 4, a [2] [4]


Sample Code:

① Pointing to an array element:

Run:



② Pointing to an array

Run:




Summary:

① Pointer concept, pointer variable, direct access and indirect access

② Basic usage of pointers

③ Pointer and one-dimensional array

④ Pointer and string

⑤ Pointer and two-dimensional array


Reference: http://blog.csdn.net/coder_pig/article/details/37754625


Copyright Disclaimer: you are welcome to reprint it. I hope you can add the original article address while reprinting it. Thank you for your cooperation.

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.