Pointer to a two-dimensional array

Source: Internet
Author: User

In chapter 4 of C ++ primer, we have encountered two-dimensional arrays and pointer content, which is a bit difficult to understand. Here is a section.

In C ++ primer, we introduce a method to access two-dimensional arrays, define a pointer to an array, and use this pointer to access two-dimensional arrays:

 
# Include <iostream> using namespace STD; int main () {int twoarray [3] [4] = {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}; int (* P) [4] = twoarray; // define a cout pointer to a one-dimensional array <"size of P is:" <sizeof (p) <Endl; // P occupies 4 bytes, it indicates that it is a pointer for (P = twoarray; P! = Twoarray + 3; ++ p) {// The first loop. Each loop traverses a row of elements for (int * q = * P; Q! = * P + 4; ++ q) {// second-layer loop. Each cycle outputs a cout element <* q <";}cout <Endl ;} return 0 ;}

Note the following points:

(1) Difference between (* P) [4] and * P [4]

(* P) [4] is a pointer to a one-dimensional array, which has four elements; * P [4] defines a pointer array containing four pointer elements. Calculate two P using sizeof () and find that the size of the first P is 4, that is, it is a pointer, and the size of the second p is 16, because it is an array containing four pointers.

(2) aboveProgram* What does P mean in the loop

To solve this problem, we should first look at the two-dimensional array representation method:

Represents the storage method of two-dimensional arrays, twoarray [0] ~ Twoarray [2] indicates the first address of each one-dimensional array, and twoarray indicates the first address of the whole two-dimensional array, it is the same address as twoarray [0] And twoarray [0] [0. However, they indicate different levels, so they cannot be mixed.

Int main () {int twoarray [3] [4] = {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11 }}; int (* P) [4] = twoarray; // defines a pointer to a one-dimensional array P ++; cout <twoarray [1] <Endl; // compare the differences between twoarray [1] and & twoarray [1] cout <& twoarray [1] <Endl; if (P = & twoarray [1]) {// It Must Be & twoarray [1] cout <"true" <Endl ;} else {cout <"false" <Endl;} return 0 ;}
 
The output content of twoarray [1] and & twoarray [1] are the same, both of which are the first address of line 1, but their meanings are different. twoarray [1]

It is an element-level, int *, and & twoarray [1] is a row-level, INT (* P) 4. That is to say, if you add 1 to twoarray [1], the pointer moves to the next element. If you add 1 to & twoarray [1], the pointer moves to the next row:

 
Cout <** (& twoarray [1] + 1) <Endl; cout <* (twoarray [1] + 1) <Endl;AboveCodeThe result is 8 in the first row and 5 in the second row. Because & twoarray [1] is Row-level, it is necessary to dereference it twice to obtain the actual element value.
 
P is a row-level pointer. It is at the same level as & twoarray [1]. Therefore, when the above code is used for judgment
If (P = twoarray [1])

The following error occurs: int (*) [4] and INT [4] have different levels of indirect addressing.

Therefore, the unreference operation on p * P will get an element-level pointer, and Q in the first program is such a pointer.

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.