Multi-dimensional array (array) and pointer access to it

Source: Internet
Author: User

First of all, in the C language,Multi-dimensional array"Is called"Array. (Next I will call it an array")
In some tutorials, I like to represent arrays in a matrix, but I prefer to represent arrays in a straight line, for example (defining arrays of int Ga [2] [3] [5]).
(If the image is too small, we recommend that you save it to your computer to enlarge it)
My understanding of arrays (Taking ga [2] [3] [5] as an example): This is an array. The array contains arrays, which are used as elements of another array.
Int Ga [2] [3] [5] = {ga_1 [1], ga_1 [2]}
= {Ga_2 [0], ga_2 [1], ga_2 [3]},
{Ga_2 [0], ga_2 [1], ga_2 [3]}
= {GA_3 [0], GA_3 [1], GA_3 [2], GA_3 [3], GA_3 [4]},
{GA_3 [0], GA_3 [1], GA_3 [2], GA_3 [3], GA_3 [4]},
... (Three are omitted here, And the last six are (GA_3 [0] ~ GA_3 [4)
{GA_3 [0], GA_3 [1], GA_3 [2], GA_3 [3], GA_3 [4]}; // This expression may be a problem !!! Welcome to note

First, let's take a look at the relationship between pointers and one-dimensional arrays:
Int one [5] = {0 };
Int * P1 = one; // the one-dimensional array is like this, right?

Then, let's look at the relationship between the pointer and the two-dimensional array:
Int two [3] [5] = {0 };
INT (* P2) [5] = two; // note the relationship and difference with the preceding one-dimensional array!

Finally, let's look at the relationship between pointers and three-digit Arrays:
Int three [2] [3] [5] = {0 };
INT (* P3) [3] [5] = three; // you should have summed up a rule?

Yes, four-dimensional arrays, five-dimensional arrays, And... are similar.

The reasons are as follows :(Keep in mind"Multi-dimensional array"Is"Array")
1. Ga [2] in the preceding three-dimensional array can be regarded as ga_1 [2], and its element is ga_1 [0] And ga_1 [1].
2. ga_1 [0] is an array, and its element is ga_2 [0], ga_2 [1], ga_2 [2], that is, ga_1 [0] can be expressedGa_1 [0][3]. LikewiseGa_1 [1][3] (pay attention to the color !);
3. Of course, ga_2 [0] can also be expressedGa_2 [0][5], likewise ...;
4. The summary expression is the long connected equation;
5. If there are other problems, it should be noted that the first pointer of the split sub-array points to what position;
5. I don't know if I have explained it correctly. Can you understand it? Please note the error !!!

Since the pointers P1, P2, and P3 are associated with arrays, access to array elements using pointers is now:
P1:

For ( Int I = 0 ; I < 5 ; I ++ )
Printf ( " One [% d] = % d \ n " , I, * (P + I ));

P2:

Int I = 0 , J = 0 ;
For (; I < 3 ; I ++ )
For (J = 0 ; J < 5 ; J ++ )
Printf ( " Two [% d] [% d] = % d \ n " , I, j, * ( * (P2 + I) + J ));

P3:

Int I = 0 , J = 0 , K = 0 ;
For (; I < 2 ; I ++ )
For (J = 0 ; J < 3 ; J ++ )
For (K = 0 ; K < 5 ; K ++ )
Printf ( " Three [% d] [% d] [% d] = % d \ n " , * ( * ( * (P3 + I) + J) + K ));

[Note: If you want to use pointers to access the array, print the addresses of each element in the array by using the array subscript, and then compare them. The control Letter of the printed Address is "% P "].

Note the following:
1. In * (P3 + I) + J) + k, when "P3 + I" is executed, the array step is 4*3*5 = 60. When "... + J. The step size is 4*5 = 20. + k "indicates the step size of" 4 ", where" 4 "indicates the number of bytes of the int type. (AboutArray step size.)
2. INT (* r) [5] = Ga [1] G [1] refers to the second ga_2 [0] address, that is, Ga [2] [3] [5] is regarded as ga_2 [2;
Int * t = G [1] [1] G [0] [0] refers to the second ga_2 [1] address, that is, Ga [2] [3] [5] is treated as ga_1 [2] [3.
(This may be a bit difficult to understand. We recommend that you perform this operation once on the machine .)

PS: It seems that the efficiency of using pointers and subscripts to access multiple-digit groups is the same. The way you use them depends on your proficiency. But the meaning of the two is not the same, please refer to the blog: http://www.cnblogs.com/ziwuge/archive/2011/10/24/2194813.html in the third template mentioned in the content.

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.