C-language two-dimensional arrays

Source: Internet
Author: User

two-dimensional arrays1. Definition of two-D arrays

Definition form: type array name [number of rows] [number of columns]

int a[2][3]; Total 2 rows 3 columns, 6 elements

2. Storage of two-D arrays

* The C language treats two-dimensional arrays as a set of one-dimensional arrays, that is, a two-dimensional array is a special one-dimensional array: its elements are one-dimensional arrays. For example, int a[2][3] can be thought of as consisting of a one-dimensional array a[0] and a one-dimensional array a[1], which all two one-dimensional arrays contain 3 elements of type int

* The order of two-dimensional arrays is stored in rows, first the elements of the first row, and then the elements of the 2nd row. For example, the order in which int a[2][3] is stored is: a[0][0]→a[0][1]→a[0][2]→a[1][0]→a[1][1]→a[1][2]

* Take a look at storage in memory, such as int a[2][2]

(Note: a[0], a[1] is also an array, is a one-dimensional array, and a[0], a[1] is the name of the array, so a[0], a[1] represents the address of the one-dimensional array)

The address of the 1> array A is FFC1, and the address of the array a[0] is also ffc1, i.e. a = a[0];

The address of the 2> element a[0][0] is FFC1, so the address of the array a[0] is the same as the address of the element a[0][0], i.e. a[0] = &a[0][0];

3> can finally come to a conclusion: a = a[0] = &a[0][0], and so on, can be derived a[1] = &a[1][0]

3. Initialization of two-D arrays

* Initialize by row

int A[2][3] = {{2, 2, 3}, {3, 4, 5}};

* initialize in order of storage (1th row, then 2nd row)

int a[2][3] = {2, 2, 3, 3, 4, 5};

* initialization of some elements

int A[2][3] = {{2}, {3, 4}};int b[3][3] = {{}, {,, 2}, {1, 2, 3}};

* If only some elements are initialized, the number of rows can be omitted, but the number of columns cannot be omitted

int a[][3] = {1, 2, 3, 4, 5, 6};int a[][3] = {{1, 2, 3}, {3, 5}, {}};

Some people may wonder why the number of rows can be omitted, but the number of columns cannot be omitted. Others may ask, can you specify only the number of rows, but omit the number of columns?

In fact, the question is simple, if we write this:

int a[2][] = {1, 2, 3, 4, 5, 6}; Wrong wording
If you do not know the number of columns the first row does not know how many are stored there is no point in the first row, the second row.

C-language two-dimensional arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.