C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (5)

Source: Internet
Author: User
Tags arrays
2.2. Pointers to two-dimensional arrays
Address of 2.2.1 Two-D array elements
To illustrate the problem, we define the following two-D arrays:
int a[3][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11}};
A is a two-dimensional array name, which has 3 rows, 4 columns, and a total of 12 elements. But it can also be understood that array a consists of three elements: a[0],a[1],a[2]. And it evenly each element is a one-dimensional array, and contains 4 elements (the equivalent of 4 columns), for example, a[0] represented by the one-dimensional array contains 4 elements of a[0][0],a[0][1],a[0][2], a[0][3]. As shown in Figure 5:
┏━━━━┓┏━┳━┳━┳━┓
A─→┃a[0]┃─→┃0┃1┃2┃3┃
┣━━━━┫┣━╋━╋━╋━┫
┃a[1]┃─→┃4┃5┃6┃7┃
┣━━━━┫┣━╋━╋━╋━┫
┃a[2]┃─→┃8┃9┃10┃11┃
┗━━━━┛┗━┻━┻━┻━┛
Figure 5.
But from the point of view of the two-dimensional array, a represents the first address of the two-dimensional array and, of course, the first address of the No. 0 row of the two-dimensional array. A+1 on behalf of the first address of line 1th, a+2 on behalf of the first address of line 2nd. If the first address of this two-dimensional array is 1000, because the No. 0 row has 4 integer elements, a+1 is 1008,a+2 1016. As shown in Figure 6.
A[3][4]
A┏━┳━┳━┳━┓
(1000) ─→┃0┃1┃2┃3┃
A+1┣━╋━╋━╋━┫
(1008) ─→┃4┃5┃6┃7┃
A+2┣━╋━╋━╋━┫
(1016) ─→┃8┃9┃10┃11┃
┗━┻━┻━┻━┛
Figure 6.
Since we see a[0],a[1],a[2] as a one-dimensional array name, we can assume that they represent the first address of their corresponding array, which means that a[0] represents the address of the No. 0 column element in line No. 0, that is, &a[0][0],a[1] is the address of the No. 0 column element in line 1th. &a[1][0], according to the address Operation rule, a[0]+1 represents the address of the 1th column element in line No. 0, that is &a[0][1], in general, A[i]+j represents the address of the J-Column element in line I, that is, &a[i][j.
In addition, in a two-dimensional array, we can also represent the addresses of each element in the form of pointers. As mentioned earlier, A[0] is equivalent to * (a+0) equivalence, a[1] and * (a+1), so a[i]+j is equivalent to * (A+i) +j, which represents the address of the array element A[i][j].
Therefore, two-dimensional array element A[i][j] can be expressed as either * (A[I]+J) or * (* (a+i) +j), which are equivalent to A[i][j] or written (* (A+i)) [j].
In addition, to add, if you write a program output print a and *a, you can find that their values are the same, this is why? We can understand this: first of all, in order to illustrate the problem, we consider the two-dimensional array as a a[0],a[1],a[2 of three array elements, and consider a[0],a[1],a[2 as an array name and a one-dimensional array consisting of 4 elements respectively. Therefore, a represents the address of row No. 0 of the array, and *a is a[0], which is the array name, or, of course, the address, which is the address of the No. 0 column element in the array No. 0 row.

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.