C-language arrays (very easy to understand)

Source: Internet
Author: User

An array is a collection of elements of the same type.

One or one-D arrays

1. One-dimensional array definition

int a[10];

This allows us to define an array a, which allocates 10 int types of space.

2. Initialization of arrays

We can initialize an array while we are defining it, for example:

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

Even we can initialize only some elements of the array, such as the following:

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

Here we only initialize the first 5 elements of the array, leaving the value of the other elements unknown.

If we initialize all the elements of the array, we can even omit the length of the arrays, and the compiler can automatically calculate the length of the array based on the number of elements initialized, but it is always good to read and maintain for other people.

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

3. Access to array elements

Access to the elements in the array can be the array name and subscript, the subscript is starting from 0, C does not check the array subscript is out of bounds, so in the array to access the elements need to be aware of, need to make a decision to set subscript, to prevent access to the array out of bounds.

int a[10];

int i;

for (i = 0; i < i++) {

A[i] = i + 1;

}

Two or two-D arrays

1. Two-D array definition

int a[6][10];

So we define a two-dimensional array, which is a matrix of 60 elements, with 6 rows and 10 columns. How to look at a two-dimensional array, you can see, first, a as a one-dimensional array, it has 6 elements, but each of its elements is an array containing 10 elements.

About the storage of a two-dimensional array in memory, such as an array of 3 rows and 6 columns It is this:

650) this.width=650; "src=" Http://my.csdn.net/uploads/201205/03/1336058275_2988.png "style=" border:none; "/>


2. Initialization of two-D arrays

As with the same-dimensional arrays, you can initialize all elements of an array as well as only some elements of the array.


3. Access to array elements

The array name plus subscript, but the subscript has two subscript, one is the row subscript, the other is the column subscript, the other same dimensional array is no different.

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9E/22/wKiom1mL7-vSpMiOAABVIF17VvU468.png-wh_500x0-wm_ 3-wmp_4-s_3302813653.png "title=" 2017-08-10_13-31-24.png "alt=" Wkiom1ml7-vspmioaabvif17vvu468.png-wh_50 "/>

This article from "Big Plum" blog, declined reprint!

C-language arrays (very easy to understand)

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.