The nature of a two-dimensional array:
int matrix[3][4];
int matrix[3][4]; ==> int [4] matrix[3]; Make type int[4]; Type int[3]; Indicates that the matrix is an array with 3 elements, and each element is of type int [4];
Essentially int[4] is a one-dimensional array with 4 int elements.
In fact, the essence of two-dimensional array name is: The first address constant of two-dimensional array (pointer constant);
Pointer plus 1 moves the length of the pointer's reference class.
Example: Double *p;
P+1 is the length of the 8-byte move.
Matrix +1 moved 32b,&matrix[3][4] + 1 moved 8B, (The matrix refers to the class is double[4],&matrix[3][4] refers to the class is double);
The C compiler does not rigorously check pointer-to-reference classes
scanf ("%lf", Matrix);
scanf ("%lf", *matrix); The two also assign values from the keyboard to the first element of the two-dimensional array. GCC runs &matrix[3][4] When a warning occurs, and *matrix does not, their two types are exactly the same:
&matrix[i][0] = &* (Matrix[i] + 0) =>matrix[i];
Matrix[i] is the first address of a two-dimensional array.
A two-dimensional array name that refers to the process of two-dimensional array elements:
MATRIX[I][J] <=> (* (Matrix + i)) [j]
* (Mareix + i):
Matrix + I will move I * sizeof (double[4]) byte space, that is, jumping I line element!!!!
Then, * descending order, is * (matrix) refers to the class to become a double, that is, matrix[i] is a pointer, is subscript I of that line of the first address, just, its refers to the class has become double!
(Matrix[i]) [j] <=> * ((Matrix[i] + j)
Matrix[i] + j actually moved J +sizeof (double) bytes, that is, a J double element space was moved within the row!
* (Matrix [i] +j), the spatial reference to which it points, is actually referring to matrix[i][j]!
About &matrix
Because the type of matrix is double (*) [4], so the &matrix refers to a double (*) [4], then &matrix + 1 should move 48 bytes.
Code inspection is as follows;
#include <stdio.h>
int main () {
int num;
Double array[10];
Double *p;
Double matrix[3][4];
DOUBLE[4] arr[3], *q = arr; This kind of writing C compiler may not accept!
printf ("%p\n", &num);
p = &array[0]; <=> p = & (* (array + 0)); <=> p = &*array;
<=> p = array;
printf ("P=%p array=%p &array[0]=%p\n", p, array, &array[0]);
Proof that the array is a pointer and that the class is the same as P:
printf ("p+1=%p array+1=%p\n", p+1, array+1);
Pointer +1 moves the "reference class" length of the pointer, because the reference class for P is a double,
Therefore, the p+1 should move 8B;
The experimental results show that array+1 and p+1 move the same number of bytes, then it can be explained
The reference class of array is double.
The matrix can be considered as the first address constant of a one-dimensional array:
Double matrix[3][4]; = = Double[4] matrix[3];
DOUBLE[4] is a type (a data type), then the definition of the matrix can be seen as:
Type matrix[3];
In this perspective, the matrix is a one-dimensional array name, based on the previous face of an array name
The discussion of the matrix is a pointer constant, and its reference class is type!
Further, the Matrix + 1 will move the length of the sizeof (type)!
That is, sizeof (Double[4])
printf ("sizeof (DOUBLE[4):%d\n", sizeof (double[4]));
printf ("Matrix:%p\t&matrix[0][0]:%p\n", Matrix, &matrix[0][0]);
printf ("matrix+1:%p\t&matrix[0][0]+1:%p\n", Matrix + 1, &matrix[0][0]+1);
Matrix + 1 Moved 32b;&matrix[0][0] + 1 moved 8B;
Note: The value of the pointer constant matrix is the same as the value of the pointer expression &matrix[0][0];
But! This is not a two-type same pointer, that is, a different class!
The reference class of matrix is double[4];&matrix[0][0] refers to the class is double!
When the C compiler handles two pointers, it treats the same without referring to the class operation!
scanf ("%lf", &matrix[0][0]); This is for the first element of the two-dimensional array from the keyboard to assign values
scanf ("%lf", Matrix); This also can do the above tasks!
At this point, the C compiler does not strictly check the pointer refers to the Class!!!
scanf ("%lf", *matrix); This way of writing will make GCC's warning disappear!
printf ("matrix[0][0]:%lf\n", matrix[0][0]);
The above experiment proves that *matrix and &matrix[0][0] types are exactly the same!
printf ("*matrix+1:%p\t &matrix[0][0]+1:%p\n", *matrix+1, &matrix[0][0]+1);
printf ("\n&matrix:%p\t &matrix+1:%p\n", &matrix, &matrix + 1);
scanf ("%lf", &matrix); This way of writing will make GCC's warning disappear!
return 0;
}
Pointers and two-dimensional arrays