The life of one-dimensional and two-dimensional arrays

1. Definition
int arr[10] defines that the type 10 is the length of the array, and the position is 0~9

int brr[3][4] defines the type to represent 3 rows and 4 columns, a total of 12 positions
                                              The first line brr[0][k] k is the value range of the column is 0~3, the same is true for other lines
                                              The first column is brr[h][0] The value range of h is 0~2, the same is true for other columns
2. Initialization 
(1) One-dimensional array
int arr[5] = {1,2,3,4,5};
int arr[5] = {1, 2, 3}; // rest defaults to 0

     arr[3] = 10 is just assignment, the value of the arr array with subscript 3 is 10


(2) The two-dimensional array is row-first, so the value of the row can be omitted when it is defined, but the value of the column cannot be omitted.

int brr [2] [3] = {{1,2,3} , {4,5,6}}
//A curly bracket inside represents the value of a line
int crr[2][3] ={1,2,3,4,5,6};
//According to the rule of row priority, start storage from the first position of the first row. When the storage is full, it will automatically jump to the next row to start storage.


int drr [2] [3] = {1,2,3};
//The rest default to 0

int err[][3] =  {{1,2,3},{4,5,6}};
int frr[2][3] ={1,2,3,4,5,6};
//Omit the row value, but the system will automatically calculate the row value 2

3. Printing

Use function calls to print:
When using function calls to print two-dimensional arrays, our parameter list is generally
void show(int crr[][4], int row, int col).
Note here that the array name of the two-dimensional array It is not a secondary pointer, therefore, the parameter list cannot be written as
void show(int **crr, int row, int col)//error
In the calling function, the formal parameter list of a one-dimensional array can be written as (int *arr) pointer type,

So what about the two-digit array, let's compare and analyze the similarities and differences between the two:

One-dimensional array, its definition is: int arr[4]; and what we can determine is:
Let's first look at the one-dimensional array, the array name in the one-dimensional array
The data type of arr is int *
arr+1 is int *
arr[0] is int 

In a two-dimensional array, int brr[3][4]; its array name and the following data types are:

brr: int(*p)[4]
brr+1: int(*p)[4]
brr[0]: int *
brr[0]+1: int *

brr [0][0]: int , we can regard a two-dimensional array as a one-dimensional array, one row is one cell, following the principle of row first.
That is, brr[0][4] is compared with arr[4], arr == brr[0], both are one-dimensional array names.
Here, we distinguish the meaning of the following array pointers and pointer arrays:
int (*p)[4];//Array pointer: the pointer to the array
int *p[4];//Pointer array: the array that holds the pointer

Additional:

  Regarding the problem of two-dimensional array out of bounds, since we can regard a two-dimensional array as a one-dimensional array, when the two-dimensional array goes out of bounds, it automatically jumps to the starting position of the next line. Both locations are the same.
#include <stdio.h>
intmain()
{
	int arr[2][3]={1,2,3,4,5,6};
	printf("%d %d\n",&arr[0][3],&arr[1][0]);
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651956&siteId=291194637