Dark Horse programmer--c Language Foundation---Two-dimensional array

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Two-dimensional arrays:

Can be seen as a special one-dimensional array, and each element of this one-dimensional array is a one-dimensional array

Definition of a two-dimensional array

Data type array name [length of first dimension (row)] [Length of second Dimension (column)];

int a[2][3];
Understanding Method:
1) This is a special one-dimensional array, one-dimensional array has two elements a[0] a[1]
A[0] It also stores a one-dimensional array with an array length of 3

2) defines a matrix of 2 rows and 3 columns

Initialization of two-dimensional arrays


1) Simultaneous initialization of the definition


(1) Full initialization

int a1[2]={1,2};

1, a special one-dimensional array, the first dimension has 2 elements
int a[2][3]={{12,23,45},{1,2,3}}; Segmentation

12 23 45
1 2 3

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

3, can omit the first dimension
int a[][3]={{1,2,3},{3,4,5},{3,4,5}}; int a[3][3]
int a[][2]={1,2,3,4,5,6,7,8}; int a[4][2]


(2) Partial initialization

In general, we used to initialize the array to 0.
int a[3][4]={1};

1 0 0 0
0 0 0 0
0 0 0 0

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

1 0 0 0
2 0 0 0
3 0 0 0

Note: The first-dimensional length of this notation can be omitted

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

1 2 3 4
5 0 0 0
0 0 0 0

Omit the first dimension

2) Define first, then initialize

int a[3][4];
First-Dimension subscript: 0 1 2
Second-dimension subscript: 0 1 2 3
A[0][0] = 1;

Traversal of two-dimensional arrays:

Accesses each element of a two-dimensional array

How do I access it?

Access by subscript

int a[3][4];

| 0 1 2 3
--------------------------------
0 | 00 01 02 03

1 | 10 11 12 13

2 | 20 21 22 23

Dark Horse programmer--c Language Foundation---Two-dimensional array

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.