How much does a two-dimensional array know

Source: Internet
Author: User


Some practical problems one-dimensional arrays can not be solved, so the introduction of two-dimensional arrays or even multidimensional arrays, here is focused on two-dimensional arrays, if the deep understanding of the two-dimensional array of multidimensional arrays will be solved.

1. Definition of two-D arrays: For example, there are 3 teams, 5 people per team, to fully express everyone can introduce a two-dimensional array, such as A[0][0] is the first team to represent the first person. Here A is the array name, the first [0] represents the first team, and the second [0] represents the first person in the first team. Based on this analogy, a two-dimensional array can be extended to a multidimensional array according to the requirements of the condition.

2. Two-d array in-memory layout: We can think of the layout of the two-dimensional array as a table, the first row of the table is the first row one-dimensional array, but the fact that the two-dimensional array in memory layout is not so, because the memory is not tabular, but the line shape. The equivalent of a ruler, the value of the variable in memory is arranged from the high address of the ruler to the address in turn, then the storage of the two-dimensional array conforms to this rule, the a[0][0 from the higher address is arranged at the lower address a[3][5] until the entire array is stored. A two-dimensional array in memory stores rules that follow the first change of the rightmost subscript, such as int *p; P=A[0][0]; ++p; So whose address is ++p? According to the above rules ++p should be *a[0][1]. The subscript in [] [] can be thought of as the first line, or it can be thought of as a column, which has no strict restrictions, as long as the logical meaning of the context is followed without changing the way it is actually stored.

3. Two-dimensional array name: First we know that the array name is the address of the first element of the array, so A is &a[0], and because A[0] is the first row of a one-dimensional array, it is also an array name, it also represents the first element of the one-dimensional array address,a[0]=& (one-dimensional array first element). Therefore, it is not difficult to understand that a two-dimensional array can be understood as a level two pointer. A+1 the two-dimensional array to the second row of the array, which is equivalent to (&a[0]+1), (&a[0]+1) is a pointer to the second row of one-dimensional array, so * (&a[0]+1) to the second row of one-dimensional array of names, that is * (A + 1) is also the name of the second row one-dimensional array, that is, a[1],* (* (a+1) +1) is the value of a[1][1].

4. Initialization of a two-D array: two-dimensional array initialization is given in the first form by giving a long list of initial values such as int a[2][2]={1,2,3,4}; the second way is int a[2][2]={1,2}, using a one-dimensional array with 2 complex elements.

5. A pointer to a two-dimensional array if there is an int a[5]; int *p=a, which is correct, p is a pointer to an integral type and initializes it to the first element of array A. For two-dimensional array int a[2][2]; int *p=a, it is wrong, because p is a variable that points to an integer, and it does not have the ability to point to an array, so it should be written like this: int a[2][2]; int (*p) [2]=a; P is a pointer to an array of 2 integer elements, which is an array pointer variable initialized to a.

6. Two-dimensional array usage in function parameters: same as one-dimensional array, two-dimensional array names actually pass through pointers to the first element of the array, because each element in a two-dimensional array is a one-dimensional array, so the compiler needs to know its number of bits. In a one-dimensional array we know that the formal parameter prototypes of a function are written in two ways, for example: void (int *p) or void (int p[]). Here we can imitate for example: void (int (*p) [2]) or void (int p[][2]), the first represents an array of pointers, it has the ability to point to a one-dimensional array with two elements, so there is no problem in receiving the arguments, and the second means that the second is an array of the form of the receive parameter , in fact, in the same vein as a one-dimensional array, it still receives the address of the array. Both of these methods should be aware that the first dimension can be changed, but the number of the second dimension can definitely be omitted, because it is only known that it can be evaluated for each subscript.

For arguments to a two-dimensional array in a function: arguments directly with the array name

This article from "Anser" blog, reproduced please contact the author!

How much does a two-dimensional array know

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.