c basic knowledge: detailed explanation of multidimensional array

Keywords: C C++ array

If an array has more than one dimension, it can be called a multidimensional array. Let's take a specific look at the relevant knowledge of multidimensional arrays.

1. How to initialize multidimensional arrays

int days[4][3];

As above, a two-dimensional array is defined. It can be regarded as an array of one-dimensional arrays.
We can initialize as follows:

int days[4][3]={
    {31,28,31},
    {30,31,30},
    {31,31,30},
    {31,30,31}
};

We can also use the input of the input device for initialization. The specific code is as follows:

#include <stdio.h>

int main(){
    int n;
	int m;
	scanf("%d%d",&n,&m);
	int a[n][m];
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			scanf("%d",&a[i][j]);
		}
	} 
	
		for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
		     printf("%d ",a[i][j]);
		}
	} 
}

The final operation interface is as follows:

So what is the storage order of multidimensional arrays in memory? (the subscript of multidimensional array also starts from zero), as shown in the following figure:

2. What does the array name of multidimensional array represent?

We know that the array name of a one-dimensional array is a pointer constant that points to the first element of the array. Multidimensional arrays are similar to them. The only difference is that the elements of the first dimension of a multidimensional array are actually another array, that is, the array name of a multidimensional array actually points to a one-dimensional array.

Let's look at a comparison to determine whether the following two pointer assignments are correct?

    int a[10];
    int b[10][10];
    int *pa = a;
    int *pb = b;

The first assignment is correct because the array name of a one-dimensional array is a pointer constant. The second assignment is incorrect because the array name of the two-dimensional array points to the second of the two-dimensional array.

We can make the following modifications:

#include <stdio.h>

int main(){
    int a[10];
    int b[10][10];
    int *pa = a;
    int (*pb)[10] = b;
}

3. Traverse two-dimensional array

The traversal of two-dimensional arrays can be divided into two types: one is to use subscripts for traversal, and the other is to use dereference for traversal.

The following subscripts are used for traversal:

#include <stdio.h>

int main(){
    int a[3][4] = {0};
    for(int i=0;i<3;i++){
    	for(int j=0;j<4;j++){
		    printf("%d ",a[i][j]);
		}
		printf("\n");
	}
}

The following dereference method is used for traversal:

#include <stdio.h>

int main(){
    int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    for(int i=0;i<3;i++){
    	for(int j=0;j<4;j++){
		    printf("%d ",*(*(a+i)+j));
		}
		printf("\n");
	}
}

Let's analyze * (* (a+i)+j) in detail, as shown in the following figure:
a+i: point to the element in line i;
*(a+i): point to the zeroth element of row i in the array;
*(a+i)+j: point to the j-th element in the i-th row of the array;
*(* (a+i)+j): the j-th element in the i-th row of the array

4. Convert a two-dimensional array into a one-dimensional array

Why is there a two-dimensional array (multi-dimensional array)?
Because in real life, many of our data itself is two-dimensional or multi-dimensional, it is natural that we have two-dimensional arrays, which is essentially for the readability of the program.

Why convert a two-dimensional array to a one-dimensional array?
This is because it is inconvenient for two-dimensional arrays to be called by functions as parameters in programs. Because the number of columns of a two-dimensional array must be specified when it is defined or passed as a function parameter, this means that only a two-dimensional array with a fixed number of columns can be passed to a function.
However, if we convert a two-dimensional array into a one-dimensional array, we do not need to fix the number of columns of the array when initializing the declaration, which makes our data more flexible to call functions.

Example:
This is the passing of a one-dimensional array:

int vector[10];
...
func1(int *vector);
func2(int vector[]);

When passing a two-dimensional array:

int matrix[3][4];
...
func1(int matix[][4]);
func2(int (*matix)[10]);

This transfer is obviously not as flexible as our one bit array, so when we need to call the function when the number of columns of our two-dimensional array is uncertain, we often need to convert the two-dimensional array into one-dimensional array, as follows:

#include <stdio.h>

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int a[n][m];
    for(int i=0;i<n;i++){
    	for(int j=0;j<m;j++){
    		scanf("%d",&a[i][j]);
		}
	}    
    
    int b[n*m];
    for(int i=0;i<n;i++){
    	for(int j=0;j<m;j++){
    		b[i*m+j] = a[i][j];
		}
	}
	
	for(int i=0;i<n*m;i++){
		printf("%d ",b[i]);
	}
    
}

Posted by gtibok on Thu, 28 Oct 2021 20:12:18 -0700