6- array

1, the array concept

In programming, for convenience of processing, the number of variables of the same type organized in an ordered form. The same set of data elements in sequence is called an array. In the C language, an array type data structure belong. An array can be decomposed into a plurality of array elements, the array elements may be configured basic data types or types. Thus according to different types of array elements, the array can be divided into various categories array of values, character array, an array of pointers, the array of structures and the like.

Array: a data sequence composed of the same type, is an ordered set.

Array element   by its position number is located (hereinafter referred to as the element array index) to distinguish.

With the array name and subscript  can be unified way to deal with all elements in the array, thereby facilitating implementation issues deal with a number of the same nature of the data.

Note: array element order does not refer to elements of the order  

2. Define a one-dimensional array

Is defined by: type specifier array name [constant expressions];

For example:   int A [10];

It represents defines an integer array, the array called a, the array has 10 elements, 10 elements are integer variables!

important point:

1) any type specifier is a basic structure data type or data type. For the same array that all the elements of the data type are the same.

2) array name is an array of a user-defined identifier. Writing rules should be consistent with the provisions of the writing identifier.

3) constant expression in square brackets represents the number of data elements, also referred to as length of the array.

4) allows for the same type of description, the plurality of arrays and a plurality of variables.

       For example: int a, b, c, d, k1 [10], k2 [20];

5) a [10], represents a array has 10 elements, note that the subscript is zero, which is 10 elements is, a [0], a [1], a [2], a [3], a [4], a [5], a [6], a [7], a [8], a [9]. Please note that do not hold, according to the above definition, there is no array element a [10].

6) C language does not allow for dynamic size of the array is defined, i.e. the size of the array does not depend on the value of a variable in the program is running. For example, the following array, this definition is not acceptable:

Common mistakes:

a float A [0];  / * size of the array is not meaningful 0 * /

 Int B ② (2) (. 3);    / * not parentheses * /

 ③ int K, A [K];     / * can not be explained by variable array size * /

The general form of array elements is: array name [index]

(Subscript may be an integer constant or integer expression.)

Rei如:   A [0] = A [5] Tasu A [7] -A [2 * 3]

          a  [ i+j ]

          a  [ i++ ]

Array elements are legitimate.

important point:

1) it is also commonly referred to as the array element index variable. You must be defined in order to use an array index variable. Only one by one using the index variable in C language, not once refer to the entire array.

When used in an array of 2) the definition of "array name [constant expression]" and used when referencing array elements' array name [index] "There is a difference.

3, citing a one-dimensional array

The general form for the initialization assignment:  type specifier array name [Constant Expressions] = {value, value, value} ......;

(1) in the array definition assigned to the initial value of the array elements. For example: int a [10] = {0,1,2,3,4,5,6,7,8,9};

(2) can only give part of the element assignment. For example: int a [10] = {0,1,2,3,4};   

A defined array has 10 elements, but only provides the initial value of the brace 5, which indicates that only five elements to the former initial value, a value of 0 after 5 elements. Do not believe it can debug it! ! Believe everything the book is better than no book! !

(3) if you want to make all the elements of an array value is 0,

 Can be written int a [10] = {0,0,0,0,0,0,0,0,0,0}; or int a [10] = {0};

(4) when the initial values ​​of all array elements, since the number of data has been determined, it can not specify the length of the array.

For example: int a [5] = {1,2,3,4,5}; may be written as: int a [] = {1,2,3,4,5};

In the second writing, the brace has the number 5, whereby the system will automatically define the length of the array 5 a

4, the definition of two-dimensional array

Form: data type array name [Constant Expression 1] [Constant Expression 2] = {} initialization data;

(1) direct branches to two-dimensional array initial value. Such as: int A [. 3] [. 4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

(2) All data may be written in a brace, the order of the elements of the array are arranged initial value. Such as:  int A [. 3] [. 4] = {} 1,2,3,4,5,6,7,8,9,10,11,12;

(3) initial values for some elements. Such as:  int A [. 3] [. 4] = {{}. 1,. 5 {}, {}}. 9;

(4) If all of the elements Ode initial value, the array may not be defined to specify the length of the first dimension, the second dimension is the length should not be spared. Such as:

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

It is equivalent to: int A [] [. 4] = {} 1,2,3,4,5,6,7,8,9,10,11,12;

 

 

 

 

 

 

 

 

Published 18 original articles · won praise 6 · views 1592

Guess you like

Origin blog.csdn.net/Desperado376/article/details/104195206