Define one-dimensional, two-dimensional, three-dimensional arrays

Source: Internet
Author: User

Define one-dimensional, two-dimensional, three-dimensional arrays

Arrays:

An array is an element of the same set of data types. Array elements are stored sequentially in memory, and the elements can be accessed by the following table (or index) of integers. Array first element subscript is 0

one-dimensional arrays

declare a simple one-dimensional array form as follows:

type name [size];

Type : Array data type

Name: array name

Size: Number of elements in an array

The array name is the address of the first element of the array

You cannot pass an entire array as a parameter to a function, but you can pass the address of an array.

Initialize array: such as int a[3] = {0,1,2}; a comma-delimited list of values (enclosed in curly braces) to initialize the array, with spaces between commas and values

when the value in the initialization list is less than the number of elements in the array, The compiler initializes the remaining elements to 0. If the array is not initialized, the array element is the same as an uninitialized variable, where the garbage value is stored, but if the array is partially initialized, the remaining elements are initialized to 0; If you initialize the list with an extra number of items, the compiler will consider it an error. You can omit the numbers in square brackets so that the compiler automatically matches the size of the array and initializes the number of items in the list.

after declaring an array, you can assign a value to an element by using an array subscript (or index)

when using arrays, prevent the array subscript from crossing bounds.

to create a reading group, declare and initialize the array with const

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


Two-dimensional arrays:

a two-dimensional array is an array of arrays. For example:

Double sales[2][4]

Initialize two-dimensional arrays:

int sales[2][4] =

{

{1,2,3,4},

{5,6,7,8}

}

Each numeric list is enclosed in curly braces. The first list data is used to initialize the first row of the array, and the second list data is used to initialize the second row of the array. If the first list is only 2 digits, only the first 2 digits of the array are initialized, and the last two elements are initialized to 0 by default. If the number of values in a list exceeds the number of elements per row of the array, an error occurs, but this does not affect the initialization of other rows.

initialization can also omit the inner curly braces, leaving only the outermost pair of curly braces. The initialization effect is the same as if the number of initialized values is guaranteed to be correct. However, if the number of initialization is not sufficient, the sequence of initialization is done sequentially, until all the values are exhausted. Elements that are not followed by a value initialization are uniformly initialized to 0.



three-dimensional array:

Int Box [2][2][2];

Typically, it takes 3 cycles to work with three-dimensional arrays.

you can think of a three-dimensional array of arrays as: box contains 2 elements, these 2 elements are 2 elements of the array, which each element contains 2 elements of an array.

                                &N Bsp                                 

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.