Two-dimensional arrays and pointers

Source: Internet
Author: User

Two-dimensional arrays and pointers, is a relatively difficult place, limited capacity, but also only to talk about their own a little understanding of this, such as a wordy slip of the place, a lot of forgive!!

Readers are recommended to read my other two articles first. Pointer http://www.cnblogs.com/wangweigang/p/8990237.html array and pointer http://www.cnblogs.com/wangweigang/p/ 8994743.html

Learn a thing, the key is to understand how it is used, on the basis of will be used to understand it, in order to use, how to do as long as strictly according to the style, do not charge the brain, first define, look at the code:

1 int a[3][3];

2 int (*p) [3];

3 p = A; three words, put a pointer to a two-dimensional array, the first sentence, tell the compiler, give me a space, 3*3 int type, OK this has memory space, There is memory space to have pointers, so every time with the pointer must understand me this pointer refers to which piece of space, otherwise it is easy to make mistakes, change is not good change, good space has, should have pointers, I this pointer, want to point to just apply for that piece of space, that is, want to point A, want to point A is what type. A look at int? That's the wrong number. The type of a is int [3] Note that this 3 is the second 3 if A is an int a[3][4] then the type of a is int [4] For what is this, because the so-called two-dimensional array, that is, each element is an array of one-dimensional array, that is, I this two-dimensional array A is An array of elements, except that each of these three element types is a one-dimensional array. Look at the picture

It is clear that the spatial arrangement of the two-dimensional array, because the pointer is used to manipulate space, so you want to use the pointer, it must be very skilled to understand how each storage type storage, and then the flexibility to manipulate the pointer. Now, if you want to point A to a, you have to judge the type of the element of a, it is clear that the type of a is int [3] that each element of a is an array of length 3 type int. This can be thought of as pointing to a one-dimensional array, because each element of a one-dimensional array is int, so a pointer to int can point to an array.     This allows you to define a pointer to a two-dimensional array in the second line of the above code, and we analyze this definition statement, int (*P) [3]; First look at the variable name p he and a * with parentheses to expand together, it shows that he first and the * Union to indicate that it is a pointer, remember, the parentheses must not be less, because the square brackets are higher than the operation priority, no parentheses P and [3] combined to indicate that P is a number of groups, which is inconsistent with the original intention So assigning a to P below will make a mistake. Well first show that p is a pointer, then he points to what, look outward, notice outside this int [3] is a type that represents an array of length 3 each element is an int, even if int [4] and int [3] are not a type, because the array is distinguished by length, type, array name. Arrays of different lengths are certainly not an array, and certainly not one of the different types. So at the time of definition, each parenthesis must be no less, and each array cannot be wrong. The last sentence assigns a to p A is actually the address of the array. So now the P-saved address of a, p points to a. This defines the completion, and establishes a connection between the array and the pointer, so that the 3*3 memory space can be manipulated by a or p to operate, so how to do it, see the code

//then the above code. A and P have been defined and established linksa[1][1] =2;//assign 2 to a[1][1], manipulate arrays by array name and subscript* (* (p+2)+1) =3;//equivalent to assigning p to a[2][1]* (* (A +1)+1) =4;//same as a[1][1] effectp[2][1] =4;//Same effect as * (* (p+2) +1)

In this code, A[1][1] must be well understood, that is, the operation coordinates are the () position of the element, remember, the array is starting from 0. The following sentence is not good to understand, it can be thought that in a one-dimensional array, a[1] and * (p+1) operation is a space. Just want to replace the square brackets with an asterisk and parentheses, specifically why later on, please readers now firmly remember that this is the use of pointers to manipulate the array, in writing code and examinations, can be in the presence of a[i][j] in the place of * (* (a+i) +j) or appear * (* (p+i) +j) Place for P[i][j]. In fact, when compiling your code, the compiler is to convert the form of the array subscript into the form of pointers, if you use * (* (a+i) +j) instead of a[i][j] just to help the compiler to complete a step of work, so you in the actual writing code, both ways can be used to see the personal habits, Not necessarily the array is the subscript, the pointer is the form of parentheses, OK, so why

From this picture you can clearly see the pointer and array of the position they represent, from the point of view of the pointer, the pointer p begins to point to the beginning of the array, and then P is the address of the array *p is going in a layer, that is, the first line, equivalent to * (p+0), 0 can be omitted ... But it's better to write, and then * (p+1) that the pointer moves back a unit and then goes in, which is equivalent to the second line. By analogy, * (p+2) is equivalent to the third line, at this time, * (P+i) is the first line of I+1, he is in the second i+1 line, and then the address of the element can be used to manipulate the location of this position, that is * * (p+0) = 2 is the first line of the first element, coordinates (0,0), Assigns 2 to it, equally equivalent to * (* (p+0) +0) = 2; Then it is understood that * (P+i) +J is the address of the element in the I+1 row j+1 column, and the addition of a * is * (* (p+i) +j) is this element. The parentheses must not be less, because the + operation priority is lower than *, if the removal is to address the address before the space represented by the operation, rather than the pointer to the space to operate. Maybe this paragraph is a bit around, I hope the reader can use some code to practice the practice, through the results of different operation output to analyze the meaning of the operation, whether and the results you want to have a gap, here I listed some code readers can also try to run and analyze

#include <stdio.h>intMainvoid){inta[3][4]={{1,2,3,4},{6,7,8,9, One},{ A, -, -}}//It means one-dimensional array with an element number of three. Their content is a[0]={1,2,3,4} a[1]={6,7,8,9} a[3]={11,12,13,14}printf"%d,%d\n", a,*a);//0 Header address and 0 rows 0 Column element addressPRITNF ("%d,%d\n", a[0],* (A +0));//0 Row 0 Column element addressprintf"%d,%d\n", &a[0],&a[0][0]);//0 Header address and 0 rows 0 Column element addressprintf"%d,%d\n", a[1],a+1);//1 Row 0 Column element address and 1 header addressprintf"%d,%d\n", &a[1][0],* (A +1)+0);//1 Row 0 column element addressprintf"%d,%d\n", a[2],* (A +2));//2 Row 0 Column element addressprintf"%d,%d\n"&a[2],a+2);//2 Beginning AddressPRITNF ("%d,%d\n", a[1][0],* (* (A +1)+0));//1 Row 0 column value of the elementPRITNF ("%d,%d\n", *a[2],* (* (A +2)+0);//2 rows of 0 column element values        return 0;}

This code is the pointer in which chapter inside there is a rough reference to the two-dimensional array and pointers, that is the time I studied the notes. Not very detailed. Readers can try to analyze the above code, may feel dizzy, but remember, when analyzing the code, according to the matrix I drew above to analyze, or you can put this matrix on the paper, and then in the analysis of the code not only by the brain, the brain to think about the confused

The following is a two-dimensional array to do parameter argument, I said in pointers and arrays, the array function parameters will become pointers, OK, now use a program to sample

  #include <stdio.h> #define M 3void Scan (    int (*a) [M],int n) {int i,j;        for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {scanf ("%d", &* (* (a+i) +j));    }}}void Transpose (int (*a) [M],int n) {int I, j, T;            for (i = 0, i < n; i++) {for (j = 0; J < N; j + +) {if (i = = j) break;                else {t = * (* (a+i) +j);                * (* (a+i) +j) = * (* (A+J) +i);            * (* (a+j) +i) = t;    }}}}void print (int (*a) [M],int n) {int i,j;        for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {printf ("%d", a[i][j]);    } printf ("\ n");    }}int Main (void) {int a[m][m];    Scan (a,m);    Transpose (A,M);    Print (A,M); return 0;}  

This program is to implement the transpose of the Matrix, respectively, with three functions to input, output and transpose the matrix. In this case, the macro definition is used, and all capital M is replaced by 3 if you don't understand it. The parameters of this three function have a two-dimensional array, well, to see the formal parameters of each function, he is a pointer to a two-dimensional array, so the argument is a two-dimensional array, this a can not take the address, because he is the address of a two-dimensional array. After a is passed, each parameter points to the array, but the parameters only know how many columns each row has, and how many rows are not known, so another argument is required to represent how many rows. In each function, I can either use the pointer plus * and the form of parentheses, or the square brackets subscript the form of an array to operate, just remember two points, 1: Array starting from 0, N-1 end, that is, the bounds of the array 2: the array name or the pointer name is the nearest representative of the row, far represents the column. Skilled in the use of arrays and pointers to the two forms of conversion, it is no longer because of the pointer method to look at a very complex, very faint situation. Also, when defining a function, the parameter points to a two-dimensional array, which can be defined by a pointer, such as the upper int (*a) [m], or int a[][m] The first square bracket can be written or not, because the compiler translates int a[][m] into the form of int (*a) [M], If you write int a[1][m] This 1 will be discarded, it doesn't make any sense, so when defining a function parameter, these two ways also look at personal hobbies, unless the problem is mandatory. Good pointers and two-dimensional arrays first come here to find the wrong or missing place to fill

Original. Reprint marked the source, thank you! ~

Two-dimensional arrays and pointers

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.