C language two-dimensional array definition and use - C language zero basic introductory tutorial

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

I Introduction to 2D array

stay C language In, a set of data is called Array, also known as one-dimensional Array, such as: String char , in fact, it is also an array, and the string is composed of multiple characters;

The two-dimensional array is similar to the one-dimensional array. The simple understanding is that the two-dimensional array is composed of multiple one-dimensional arrays. The syntax is as follows:

type arrayName [ x ][ y ];
//Value range of X: 0 < = index < x subscript starts from 0, and the maximum value is x-1, otherwise the subscript will be out of bounds
//Value range of Y: 0 < = index < y subscript starts from 0, and the maximum value is y-1, otherwise the subscript will be out of bounds

Therefore, each element in the array is identified by the element name of the form a [i, J], where a is the array name and i and j are the subscripts that uniquely identify each element in a.

Two dimensional array Each data in the array is called an array Element. Each Element in the two-dimensional array has a sequence number, which is composed of x and y (i.e. horizontal coordinates and vertical coordinates), starting from 0. For example, a[0][6] represents the 7th Element in line 1, and a[3][1] represents the 2nd Element in line 4;

For example: define such a two-dimensional array

int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}};

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-vho32hip-1629616389974)( https://www.codersrc.com/wp-content/uploads/2021/06/e4da3b7fbbce234.png "C language two-dimensional array definition and use - Ape programming")]

Two dimensional array It is essentially a one-dimensional array. If each row of a two-dimensional array is regarded as a whole, that is, as an element in an array, the whole two-dimensional array is a one-dimensional array array.

II Define a two-dimensional array and initialize it

1. Define a two-dimensional array

To put data into memory, memory space must be allocated first. For example, put 4 one-dimensional arrays and 8 integers, that is, 4 rows and 8 columns:

//Shaping array
int a[4][8];//It is composed of four one-dimensional arrays with index values of 0 ~ 3; Each one-dimensional array has 8 shaping variables with index values of 0 ~ 7;
//Floating point array
float a[4][8];//It is composed of four one-dimensional arrays with index values of 0 ~ 3; Each one-dimensional array has 8 floating-point variables with index values of 0 ~ 7;
//Floating point array
double a[4][8];//It is composed of four one-dimensional arrays with index values of 0 ~ 3; Each one-dimensional array has 8 floating-point variables with index values of 0 ~ 7;
//character string
char a[4][8];//It is composed of four one-dimensional arrays with index values of 0 ~ 3; Each one-dimensional array has 8 character variables with index values of 0 ~ 7;

2. Initialize 2D array

A. Assignment while defining a two-dimensional array

Multidimensional arrays can be initialized by specifying values for each row in parentheses. Here is an array with 3 rows and 4 columns.

int a[3][4] = {
                {0, 1, 2, 3} ,   /*  Initializes the row with index number 0 */
                {4, 5, 6, 7} ,   /*  Initializes a row with index number 1 */
                {8, 9, 10, 11}   /*  Initializes the row with index number 2 */
};

Internally nested parentheses are optional. The following initialization is the same as the above:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

B. Assign values to array elements based on two-dimensional array subscripts

When modifying the value of a two-dimensional array by subscript, you should pay attention to subscript out of bounds The problem of;

a[4][2];  //Define a two-dimensional array
a[0][0]=10;
a[0][1]=20;
a[1][0]=30;
a[1][1]=40;
a[2][0]=50;
a[2][1]=60;
a[3][0]=70;
a[3][1]=80;
//a[0][3]=20; // Wrong writing, subscript out of bounds
//a[0][2]=20; // Wrong writing, subscript out of bounds
//a[4][2]=20; // Wrong writing, subscript out of bounds
 Equivalent:
a[4][2]= {10,20,30,40,50,60,70,80}
Equivalent:
a[4][2] = {
                {10,20},
                {30,40},
                {50,60},
                {70,80}
}

C. Only initial values are assigned to some elements. When the number of values in {} is less than the number of elements, only the previous elements are assigned values, and the latter elements are 0 by default

//Case 1
int a[3][4] = {{1},{2},{3}};
Equivalent:
int a[3][4] = {{1,0,0,0},{2,0,0,0},{3,0,0,0}};
//Case 2
int a[3][4] = {{1}};
Equivalent:
int a[3][4] = {{1,0,0,0},{0,0,0,0},{0,0,0,0}};

When the assigned elements are less than the total elements of the array, the remaining elements are automatically initialized to 0:

about short,int,long,Is the integer 0;
about char,It's a character '\0';
about float,double,It's the decimal 0.0. 

III Accessing 2D arrays

**Each element in the array has a sequence number. This sequence number starts from 0 and is called the subscript index. For example, a[0][2] represents the third element in line 1, and a[3][5] represents the sixth element in line 4. Therefore, we can directly access the values of elements in the array through subscripts, * * for example:

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com com
//@File:C language tutorial - C language two-dimensional array definition and use
//@Time:2021/06/12 08:00
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/

#include <stdio.h>
int main(void) {
    int a[3][4] = {{1,2,3,4},{2,4,6,8},{3,6,9,0}};
    for (int i = 0 ;i<3;i++) {
        for(int j = 0;j<4;j++)
            printf("a[%d][%d] = %d  ",i,j,a[i][j]);//Query by subscript
        //Line feed
        printf("\n");
    }
    return 0;
}
/*
Output:
a[0][0] = 1  a[0][1] = 2  a[0][2] = 3  a[0][3] = 4
a[1][0] = 2  a[1][1] = 4  a[1][2] = 6  a[1][3] = 8
a[2][0] = 3  a[2][1] = 6  a[2][2] = 9  a[2][3] = 0
*/

IV Modify 2D array

**Since we can access the value of the array through the array subscript, we can also modify the value of the array using the subscript, * * for example:

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com com
//@File:C language tutorial - C language two-dimensional array definition and use
//@Time:2021/06/12 08:00
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/

#include <stdio.h>
int main(void) {
    int a[3][4] = {{1,2,3,4},{2,4,6,8},{3,6,9,0}};
    for (int i = 0 ;i<3;i++) {
        for(int j = 0;j<4;j++)
            printf("a[%d][%d] = %d  ",i,j,a[i][j]);//Query by subscript
        //Line feed
        printf("\n");
    }
    printf("-------------------------------------------\n");
    //Modify the value of the array
    for (int i = 0 ;i<3;i++) {
        for(int j = 0;j<4;j++)
            a[i][j] *= 10 ;//Equivalent a[i][j] = a[i][j] * 10
        //Line feed
        printf("\n");
    }
    //Query the value of the modified array
    for (int i = 0 ;i<3;i++) {
        for(int j = 0;j<4;j++)
            printf("a[%d][%d] = %d  ",i,j,a[i][j]);//Query by subscript
        //Line feed
        printf("\n");
    }
    return 0;
}
/*
Output:
a[0][0] = 1  a[0][1] = 2  a[0][2] = 3  a[0][3] = 4
a[1][0] = 2  a[1][1] = 4  a[1][2] = 6  a[1][3] = 8
a[2][0] = 3  a[2][1] = 6  a[2][2] = 9  a[2][3] = 0
-------------------------------------------
a[0][0] = 10  a[0][1] = 20  a[0][2] = 30  a[0][3] = 40
a[1][0] = 20  a[1][1] = 40  a[1][2] = 60  a[1][3] = 80
a[2][0] = 30  a[2][1] = 60  a[2][2] = 90  a[2][3] = 0
*/

V Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 uninstall
  5. C language format controller / placeholder
  6. C language logical operators
  7. C language ternary operator
  8. C language comma expression
  9. Differences between sizeof and strlen functions in C language
  10. C language strcpy and strcpy_s function difference
  11. C language memcpy and memcpy_s difference
  12. C language array definition and use
  13. C language array traversal
  14. C language array sorting - bubble sorting
  15. C language array sorting - selection sorting
  16. C language array sorting - insertion sorting
  17. C language array sorting - quick sorting
  18. C language array subscript out of bounds
  19. C language array memory overflow
  20. Difference between C language array subscript out of bounds and memory overflow
  21. Definition and use of two-dimensional array in C language

No reprint without permission: Ape programming » Definition and use of two-dimensional array in C language

Added by firelior on Fri, 24 Dec 2021 12:42:38 +0200