Two-dimensional array
Talk before the first two-dimensional array is an array of pointers and pointer and array of next-dimensional array
First, the difference between one-dimensional array of array pointer and the pointer array
Array of pointers:
1 int * p [5];
[] * Higher priority than the first which is an array whose size is 5, the type of data which is stored int *, which is an integer pointer. Therefore, it is called an array of pointers, say in the end that p is an array of elements in the array is a five pointer, each pointer in the array point to a variable of type int
An array of pointers:
int (* p) [5];
First, p is a pointer to an array of size 5, so called pointer array, a pointer to define the one-dimensional array of five elements. (Priority parentheses)
Second, the difference between the two at the time of assignment
Assign an array of pointers
#include <stdio.h>
main()
{
int *p[5];
int a = 10;
p[0] = &a;
printf("%d", *p[0]);
}
Pointer array assignment
main()
{
int (*p)[5];
int a[5] = {1, 2, 3, 4, 5};
p = &a;
printf("%d", *p[0]);
}
Third, on the address of the array (here only discuss one-dimensional \ two-dimensional array)
One-dimensional array
int a[5];
a represents the first array of addresses, a is equivalent to & a [0]
Two-dimensional array
int a[2][2] = {1, 2, 3, 4};
A first address represented by the entire array, a [0] indicates the first address of the first row, both are the same in value, but the meaning of different (or different types), a name for the entire array array, a [0] for the first row
Address both (a, a [0]) to verify whether the same
#include <stdio.h>
int main()
{
int a[2][2] = {1, 2, 3, 4};
printf("%p\n%p\n", a, a[0]);
return 0;
}
operation result
When the address assignment with the array, although the same three values, but not free to mix the three (in int a [2] [2] as an example)
is a -------- int (*) [2] Type
a [0] ----- is type int *
For a [0] and & a [0] [0], two types are int * type, the following two methods is equivalent to assignment
The first:
The first:
1 int a[2][2] = {1, 2, 3, 4};
2 int *p;
3 p = a[0];
The second:
1 int a[2][2] = {1, 2, 3, 4};
2 int *p;
3 p = &a[0][0];
For int a [2] [2], if the a [0] to & a [0], then & a [0], and the same type of a, are int (*) [2] type, below to int a [5] [5], for example, lists the different types of two-dimensional array elements in different ways of expressing
You can also use a one-dimensional array of pointers to save the address of a two-dimensional array of elements
1 int a[2][2] = {1, 2, 3, 4};
2 int *p[2];
3 p[0] = &a[0][0];
4 printf("%d", *p[0]);
Fourth, the solution of two-dimensional array of references
A two-dimensional array a [2] [3] = {1, 2, 3, 4, 5, 6}; Example (first dimension is the row, the second dimension is the column)
The first: * (* a + 1) -------- is equivalent to a [0] [1], since priority than + * high, the first reference solution, into the second dimension of the inside the two-dimensional address +1 dereferencing again to get the element values
The second: * (* (a + 1)) ------ equivalent to a [1] [0], to pay more than the above of a first bracket, bracket highest priority, to the mobile address +1 (Note that there is movement in the first dimension), then dereferencing into the second dimension, the element values obtained dereferences then
Third: * (& a [0] [0] +1) - equivalent to a [0] [1], is used here to take the address & character, originally represents the first element of a [0] [ 0] returns to the second dimension, the second dimension and the address + 1, the element values obtained dereferences then
For the convenience of the reader understand the figure below
supplement:
Please look at the following code reader
#include <stdio.h>
main()
{
int a[2][3] = {1, 2, 3, 4, 5, 6};
printf("%d***%d", *(a[1]+1), (*a+1)[1]);
}
* (A [1] +1) -------- represents the value of a [1] [1]
(* A + 1) [1] -------- it is represented by a [0] [2] value
For ease of description to return a one-dimensional array to int a [5], the first address of a representation of the array a, a [2] represents a mobile two addresses on the basis of a (a note type of type int * ) and then dereferencing obtained value of the element, meaning that a [2]
Actually consists of two steps, the first step in moving address, dereferences the value of the second step to obtain elements (note the second step, a little implicit conversion means, often overlooked)
Now to explain the above two-dimensional array is much easier
The first first look * (a [1] +1), a [1] first address representative of the second line, pay attention to the dimensions here is the second dimension, the second dimension and priority brackets address +1, and finally Solutions of reference values obtained element
The second term (* a + 1) [1], a mention here, because [] * is higher than the priority of the bracket where it can not be removed, the first step to enter the second dimension dereferencing (Priority * level above +), and then the second-dimensional address + 1, and then move again on the basis of the current address and then, finally dereferencing
得到元素的值,这里可能有点绕,换个说法就是[1]是在当前维度进行移动,然后解引用(“当前维度”有点不太严谨,为了方便理解先将就这么用了)
拿a[2][1]来说,一共有四步,其中包含了两次地址移动,两次解引用,执行顺序是:地址移动->解引用->地址移动->解引用(这里提一句,[]的结合性是左结合的,所以在移动的时候先移动行(第一维)再移动列(第二维),小声BB)
详细步骤:第一步:在当前维度地址+2,因为a的维度是第一维,所以是第一维地址+2,即行+2
第二步:解引用进入第二维度
第三步:在当前维度地址+1,因为这时已经进入第二维,所以第二维地址+1,即列+1
第四步:解引用得到元素的值