Home  >  Article  >  Backend Development  >  How to define two-dimensional array

How to define two-dimensional array

anonymity
anonymityOriginal
2019-05-06 17:23:5769256browse

A one-dimensional array has only one subscript, which is called a one-dimensional array, and its array elements are also called single-subscript variables. In practical problems, many quantities are two-dimensional or multi-dimensional, so the C language allows the construction of multi-dimensional arrays. Multidimensional array elements have multiple subscripts to identify their position in the array, so they are also called multi-subscript variables. This section only introduces two-dimensional arrays. Multi-dimensional arrays can be derived by analogy with two-dimensional arrays.

How to define two-dimensional array

Definition of two-dimensional array

The general form of two-dimensional array definition is:
Type specifier Array name [constant expression 1] [constant expression 2]
where constant expression 1 represents the length of the first dimension subscript, and constant expression 2 represents the length of the second dimension subscript. For example: int a[3][4];
describes an array with three rows and four columns. The array name is a, and the type of its subscript variable is integer. There are 3×4 subscript variables in this array, namely:
a[0][0], a[0][1], a[0][2], a[0][3]
a[1][0], a[1][1], a[1][2], a[1][3]
a[2][0], a[2][1] , a[2][2], a[2][3]
The two-dimensional array is conceptually two-dimensional, that is to say, its subscript changes in two directions, and the subscript variable in the array The position is also in a plane, not just a vector like a one-dimensional array. However, actual hardware memory is continuously addressed, which means that the memory cells are arranged linearly in one dimension. There are two ways to store a two-dimensional array in a one-dimensional memory: one is to arrange it in rows, that is, after one row is placed, the second row is placed sequentially. The other is to arrange by column, that is, after placing one column, put it in the second column.
In C language, two-dimensional arrays are arranged in rows. That is, the a[0] row is stored first, then the a[1] row is stored, and finally the a[2] row is stored. The four elements in each row are also stored sequentially. Since array a is described as int type, this type occupies two bytes of memory space, so each element occupies two bytes.

Reference of two-dimensional array elements

The elements of two-dimensional array are also called double subscript variables, and their representation is in the form:
Array Name[subscript][subscript]
The subscript should be an integer constant or an integer expression. For example:
a[3][4]
represents the elements in three rows and four columns of array a.

The above is the detailed content of How to define two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn