[C language] multidimensional arrays

C language support multidimensional arrays. The general form of a multidimensional array declaration is as follows:

type name[size1][size2]...[sizeN];

Two-dimensional array

The simplest form of a multidimensional array is a two-dimensional array. A two-dimensional array, in essence, is a list of a one-dimensional array. Declaring a column x row y 2D integer array form as follows:

type arrayName [ x ][ y ];

Wherein, type  can be any valid C data type, arrayName  is a valid C identifiers. A two-dimensional array can be thought of as a table with rows x and y columns. The following is a two-dimensional array, rows 3 and 4 comprising:

int x[3][4];

Two-dimensional array in C

Thus, each element of the array is used to form a [I, j] to identify the name of the element, wherein a is an array name, i and j are uniquely identified in a subscript of each element.

Two-dimensional array initialization

Multi-dimensional array can be initialized to the specified values ​​in each row in parentheses. The following is an array with three rows and four columns.

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

or

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

Access two-dimensional array elements

A two-dimensional array elements are accessed by the subscript (ie, the array row index and column index). E.g:

int val = a[2][3];

example:

#include<stdio.h>
int main()
{
    / * An array of 3 rows with 4 * / 
    int A [ 3 ] [ . 4 ] = { . 1 , 2 , 3 , . 4 , . 5 , . 6 , . 7 , . 8 , . 9 , 10 , . 11 , 12 is };
     int I , J;
     / * output value of each element of the array * / 
    for (I = 0 ; I < . 3 ; I ++ )
    {
        for (j = 0; j < 4; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

result:

 

 

Guess you like

Origin www.cnblogs.com/HGNET/p/12030927.html