A two-dimensional array of C + + Inverse-day grammar series

Source: Internet
Author: User

A company's pen test, its own analysis of the answer, and in the back of the two-dimensional array element representation form to summarize. Novice friends do not nag, patience to read and remember that you will be helpful. Veteran veterans are also welcome to comment.


int A[4][4] = {{1,2,3,4},{50,60,70,80},{900,1000,1100,1200},{13000,14000,15000,16000}};int (*P1) [4] = A;int (*P2) [4] = &a[0];int *p3 = &a[0][0];p rintf ("%d,%d,%d,%d\n", * (* (a+1)-1), * (* (P1+3)-2) +1,* (* (p2-1) +16) +2,* (P3+sizeof (P1 )-3);

64-bit environment, ask the running result of the above code.


Well, to parse, the first two-dimensional array name points to the first line of the two-dimensional array,

int (*P1) [4]=a, is a pointer to an array of four integers;

int *p3=&a[0][0], a two-dimensional array is referenced by a one-dimensional array;

First output: A+1 is the address of the second row, * (a+1) is the address of the first element of the second row, minus one is the address of the element in front of it, that is, the fourth element in the first row, and the result is 4.

The second output: P1 and P2 and A are the same, p1+3 is the address of line fourth, * (P1+3) The address of the first element of line fourth, minus two is the address of the third element of the third row, and the result is 1101.

The third output: P2-1 is the fourth line address, * (p2-1) represents the first element of the fourth line address, plus 16 is a circle or this element, the result is 13002.

Fourth output: 64-bit environment, then sizeof (p1) is 8,p3+5 represents the fifth element of a one-dimensional array, the second element of the second row of a two-dimensional array, and the result is 60.


Well, let's make a summary:

The two-dimensional array element A[i][j] has the following types of references:

* (a[i]+j) //a[i] The address of the group I, plus J, which is the array I row J column address

* (* (a+i) +j) //a+i Address of Group I, * (a+i) to row 0 of array I, plus J for Row J of Line I address


A two-dimensional array of C + + Inverse-day grammar series

Related Article

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.