Address of a two-dimensional array element

Source: Internet
Author: User

The address of a one-dimensional array element is easy to understand, but for a two-dimensional array, it is easy to confuse. Today I was confused by this problem, turned over the old Tan books, the problem has a deeper understanding.

First, we give a two-dimensional array A, which is defined as:

int A[3][4] = {{1,3,5,7}, {9,11,13,15}, {17,19,21,23}};

The A array contains 3 line elements, a[0],a[1],a[2]. Each row element is a one-dimensional array that contains 4 elements.

From the perspective of a two-dimensional array, a represents the address of the first element of a two-dimensional array, and now the first element is not a simple integer, but a one-dimensional array of 4 integral elements, so a represents the first address of the first row (the row with the ordinal 0). Similarly, A + 1 represents the first address of the line with the ordinal 1. If the first row of the two-dimensional array has a first address of 2000 and an integer data of 4 bytes, then the value of a+1 should be 2000+4*4=2016.a+1 point to a[1], or a+1 is the first address of a[1], * (a+1) equivalent to a[1].

A[0],A[1],A[2] Since it is a one-dimensional array name, A[0] represents the address of the No. 0 column element in a one-dimensional array a[0], that is, the value of &a[0][0]; a[1] is &a[1][0],a[2] is &a[2][0];

So what is the address of an array of 0 rows and 1 column elements? A[0] is a one-dimensional array name, and the address of the element with the ordinal 1 in the one-dimensional array should obviously be represented by a[0]+1, such as.

Previously described, a[0] and * (a+0) equivalent, a[i] and * (a+i) equivalent. Therefore a[0]+1 and * (a+0) +1 are &a[0][1]. Further analysis, the value of a[0][1] is * (a[0]+1), * (* (a+0) +1) or * (*a+ 1).

   Generic representation: * (A[I]+J), * (* (a+i) +j) is the value of A[i][j] . It can be understood as follows: The row pointer a moves vertically I to reach a+i, then the dereference gets * (A+i), that is, the a[i]--column pointer, and then the column pointer a[i] lateral move of the J-Step Arrival (A[i]+j)), the last solution is quoted (A[I]+J), that is, a[i][j];

See the table below for more examples.

An interesting phenomenon is that a+1 and * (a+1) are all 2016, it is not difficult to understand, they are the address, a+1 points to a in the sequence number 1 of the first address of the line, and * (A+1) is a[1], pointing to the a[1][0], the two are actually the same position.

In the book with a military training squad leader and platoon name of the problem vividly illustrate this kind of problem.

A row of 3 classes, each class stood in a row, this is equivalent to a two-dimensional array. For comparison, the class and Warrior's serial number also starts from 0. The monitor named the man-made unit, starting from the No. 0 warrior, and moving one step through a warrior. The platoon name takes the class as the unit, checks whether the class is in line, The platoon leader walks through a class of soldiers every step of the way. The monitor's movement is horizontal, the platoon leader's movement is longitudinal. The monitor "points" the warrior, the platoon leader "points" the class, the monitor corresponds to the column pointer, the platoon commander equals the row pointer.

Address of a two-dimensional array element

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.