1. Concept
If an array has more than one dimension, it is called a multi-dimensional array. For example, the following statement:
Int matrix [6] [10];
Creates a matrix containing 60 elements. But is it 6 rows with 10 elements per line, or 10 rows with 6 elements per line?
To answer this question, you need to inspect multidimensional arrays from a different perspective. Consider the following statements with increasing dimensions:
Int;
Int B [10];
Int c [6] [10];
Int d [3] [6] [10];
A and B are easy to understand. C is only adding one dimension on the basis of B. Therefore, we regard c as a vector containing six elements, except that each element itself is a vector containing 10 elements. In other words, c is a one-dimensional array. D is also true: it is a vector containing three elements, and each element in the three elements is itself a vector containing six elements, each of these six elements is a vector containing 10 elements.
2. Storage Sequence
Remember that multi-dimensional arrays are only a representation method, while in actual memory space storage, they are stored in order.
For example, int [2] [5];
In the memory, 10 small areas are arranged in order.
Let's take a look at the example below:
Int matrix [6] [10];
Int * mp;
.....
Mp = & matrix [3] [8]
Printf ("First value is % d \ n", * mp );
Printf ("Second value is % d \ n", * ++ mp );
Printf ("Third value is % d \ n", * ++ mp );
Obviously, the first printed value is the content of matrix [3] [8]. What is the next printed value? Ordered storage can answer this question-the next element will be the first change of the rightmost subscript, that is, matrix [3] [9]. Who is next? The 9th column is the last column. However, according to the sequential storage, when a row is full, it is the next row. Therefore, the next printed element will be matrix [4] [0].
2. array name
The array name of a one-dimensional array is a pointer constant, which should be clear to everyone. Its type is "pointer to the element type", which points to the first element of the array element. The multi-dimensional array is similar, but this "almost" often leads to some misleading information for beginners. The array name of the multi-dimensional array is also a pointer, this Pointer Points to the first "element" of the array, which is also an array. For example, the following statement:
Int matrix [3] [10];
Next, let's look at the following expression:
Matrix [1] [5]
This is very simple. This expression indicates the elements contained in column 6 of the second row of the array.
Matrix + 1
If you think this expression points to the second element of the first line, you will be wrong. In fact, this expression points to the entire array of the second row, rather than any element. Www.2cto.com
* (Matrix + 1) is a pointer to the first element in the second row. * (Matrix + 1) indicates the specific value of the first element in the second row.
In fact, I feel that the design method of multi-dimensional arrays in C language is not very appropriate. This method is not closely related to the rules of one-dimensional arrays, so it is easy to understand, matrix + 1 indicates that the second element of the first line is easier to understand. However, it may be because of the simplicity of the code that the C language inventor uses such an "automatic line feed" process.
TIPS:
In many other languages, such as the familiar C #, multiple subscripts are written in the form of commas. For example:
Matrix [3, 4];
What will happen if you write in C language like this? Your first thought must be: Compilation error. However, it is a pity that such expressions can be compiled in C Language (remember 2 [array? The flexibility of C language is incomparable to that of other languages ).
It seems that there is no problem. The actual running results of such expressions will be different from what you expected.
The comma expression in C Language converts it to matrix [3].
If the compiler can find this error, it is a pity that the compiler cannot find this error due to the existence of the comma expression.
3. pointer to array
Are the following statements legal?
Int vector [10], * vp = vector;
Int matrix [3] [10], * mp = matrix;
The first statement is legal. It allocates memory for an integer array, declares vp as a pointer to an integer, and initializes it to the first element of the vector array.
The second statement is invalid. It correctly creates a matrix array and declares mp as a pointer to an integer. However, the initialization of mp is incorrect, because matrix is not a pointer to an integer, but a pointer to an integer array. How should we declare a pointer to an integer array?
Int (* p) [10];
This statement is a little complex but correct. After Initialization is added to the declaration, it looks like the following:
Int (* p) [10] = matrix;
It points p to the first line of matrix.
P is an array pointer pointing to 10 integer elements. When you add p to an integer, the integer is adjusted based on the length of 10 integers before addition. So we can use this pointer to move a row in the matrix.
If you need a pointer to access the integer elements one by one instead of moving them row by row in the array, we can declare:
Int * pi = & matrix [0] [0];
Int * pi = matrxi [0];
4. Initialization
There are two methods to initialize multi-dimensional arrays:
Int matrix [2] [3] ={ 100,101,102,103,104,105 };
Int matrix [2] [3] = {
{100,101,102 },
{1, 103,104,105}
};
There is no difference in nature between the two, but the second method of initialization is more readable.
From Kernel & UI