Two-dimensional array in C language

One-dimensional arrays

One-dimensional array array names refer to the entire array in the following two cases:

1. In the same function that defines the array, find sizeof

2. In the same function that defines the array, &arr+1

In other cases, it means the first address of the array

Two, two-dimensional array

Since the array name of a one-dimensional array generally represents the first address of the array, does the array name of a two-digit array also represent the first address of the array?

Two-dimensional arrays are essentially arrays with arrays as array elements, ie "arrays of arrays"

It can be seen from the source code below

#include <stdio.h>
 
intmain()
{
    int array[3][5] = {0};//Define a two-dimensional array (3 rows and 5 columns)
    int temp = 0;//Set a temporary integer variable to assign values ​​to the array
    for (int a = 0; a < 3; a++)//The outer loop assigns the first dimension of the array, which is the x of array[x][y]
    {
        for (int b = 0 ; b < 5; b++)//The inner loop assigns the second dimension of the array, which is the y of array[x][y]
        {
            temp = temp + 1;//In order to make the value of the array different, let the temporary variable have an auto-increment
            array[a][b] = temp;//The real data of the two-dimensional array
            printf("array[%d][%d] = %-4d",a,b,array[a][b]);//Print out the array
        }
         printf("\n");//Line break after outputting a line
    }
    return 0;
}

The output is:


Can a two-dimensional array be the same as a one-dimensional array, and can pass the first address of the array name to a function like a one-dimensional array?

The answer is no, because one-dimensional arrays are just arrays, and two-dimensional arrays are arrays of arrays.

We can see from below

We discuss from left to right, top to bottom,

arr is the name of the one-dimensional array array at this time, that is, the first address of the array, that is, the pointer, the variable type is int *

arr+1 uses pointer addition at this time (you can see the use of my pointer), the variable type is int *

arr[0] is int data at this time

brr is the array name of the two-dimensional array at this time, that is, the first address of the array of the array, that is, the pointer of the array, and the variable type is int (*p)[4]

(brr is an array of three (arrays of length four), so the type is int (*p)[4] , or int (*p)[5] if the length is five )

brr+1 uses pointer addition at this time, and the variable type is int (*p)[4]

brr[0] can be understood as the array name of a one-digit array at this time, and the variable type is int *

brr[0]+1 uses pointer addition at this time, and the variable type is int *

brr[0][0] is int data at this time

Third, pay attention to distinguish between array pointers and pointer arrays

Array pointer: pointer to the array (not the first address of the array)

int (*p)[4]

array of pointers: an array of pointers

int *p[4]

Guess you like

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