C language programming study notes-array,

Source: Internet
Author: User

C language programming study notes-array,

C language programming learning notes-array.

Array 6.1

(1) It is a set of ordered data. The data element types are the same, and they are stored sequentially, occupying adjacent storage space.

(2) The array must be defined first and then referenced

(3) the C language can only reference array elements one by one and cannot reference the entire array at a time.

(4) The array reference is an array name + subscript reference, and the array subscript is an integer.

(5) When defining static array variables, the compiler automatically initializes the data element to 0,

Dynamic Array variables are allocated storage space during program execution and their values are random before being assigned a value for initialization.

6.2 one-dimensional array:

1. Definition: type character array name [constant expression]

2. Description: (1) constant expression is used to describe the length of the array, starting from a [0.

(2) It can contain constants and symbolic constants. a [5 + 3] is legal and cannot be dynamically defined on the array size.

(3) Variable Length array. The array expression defined in the called function (excluding the main function) can be a non-constant expression. The call time is variable and the execution time remains unchanged. If the array is static storage, it is unavailable.

Static int a [2 * n] unavailable

3. Reference: array name [subscript]

4. Initialization: assign values to array elements while defining Arrays

(1) assign values when defining

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

(2) assign values to only some elements

Int a [4] = {1, 2, 6 };

(3) If all array values in an array are 0

Int a [6] = {0 };

(4) When assigning values to all elements, the number is determined and the length can be left blank.

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

6.3 two-dimensional array

1. Definition: type character array name [constant expression] [constant expression]

2. Description: (1) Two-dimensional arrays are stored by row.

3. Reference: array name [subscript] [subscript]

4. Initialization:

(1) initialize the branch

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

(2) Non-branch Initialization
Int a [2] [3] = {1, 2, 3, 4, 5, 6 };

(3) initialize some array elements
Static inta [2] [3] = {1, 2}, {4 }};
The first row has only two initial values, which are assigned to a [0] [0] And a [0] [1] in order. the initial value 4 of the second row is assigned to a [1] [0]. Because the storage type is static, the initial values of other array elements are 0. Note: In some C language systems (such as TurboC), the initial values of variables or arrays whose storage type is not static are also 0.
Static int a [2] [3] = {1, 2 };
There are only two initial values, that is, a [0] [0] = 1, a [0] [1] = 2, and the initial values of other array elements are 0.
(4) The definition of the first dimension can be omitted, but the definition of the second dimension cannot be omitted. The system determines the length of the first dimension based on the number of initialized data and the length of 2nd dimensions.
Int a [] [3] = {1, 2, 3, 4, 5, 6 };
The definition of the first dimension of array a is omitted. There are 6 initial data records. The length of the second dimension is 3, that is, the number of three rows. Therefore, the first dimension of array a is 2.

6.4 character array

1. Definition:

An array used to store the character quantity is called a character array. Char c [10]

2. Reference:

Reference an element in the character array to reference a character.

3. String and string end sign:

? In C, strings are processed as character arrays.

? The C system automatically adds '\ 0' as the terminator when storing string constants in character arrays. For example, "C program" has a total of 9 characters. A string is stored in a one-dimensional array, which occupies 10 bytes. The last byte '\ 0' is automatically added by the system. With an ending sign '\ 0', the length of the character array is not that important. In programs, it is often used to determine whether the string ends by detecting the '\ 0' position, rather than determining the length of the string based on the length of the array.

4. Input and Output of character Arrays:

(1) character-by-character input, for example

Char a [5];

For (int I = 0; I <5; I ++)

{

Scanf ("% c", & a [I]); // Note: The address character cannot be missing &

// A [I] = getchar ();

}

(2) Use scanf function input, such

Char a [5];

Scanf ("% s", a); // Note: The address operator & cannot be added because the array name is an address.

Note:

? There is no address character before &

? Scanf is used for input. If a space is entered OR a carriage return OR Tab character is entered, the input ends. If we enter AB cde on the keyboard, the value of array a is AB.

? Use the gets function input, for example

Char a [5];

Gets ();

Note:

The gets () function is used for input. The input is complete only when a carriage return occurs. If we enter AB cde from the keyboard, the value of a is AB cde.

?Use string processing:

(1) puts

(2) gets

(3) strcat

(4) strcpy and strncpy

(5) stremp

(6) strupr

?Initialization:

(1) assign values to each element.

Char c [0] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};

The values of each element after the assignment are:

The value of c [0] Is 'C'

The value of c [1] is''

The value of c [2] is 'P'

The value of c [3] is 'R'

The value of c [4] Is '0'

The value of c [5] is 'G'

The value of c [6] is 'R'

The value of c [7] is 'A'

The value of c [8] Is 'M'

C [9] is not assigned a value, which is automatically assigned a value of 0 by the system. The length description can be omitted when an initial value is assigned to all elements. For example:

Char c [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M '};

The length of the C array is automatically set to 9.

(2) Use a string to initialize and assign values to the array. For example:

Char c [] = {'C', '', 'P', 'R', 'O', 'G', 'R', 'A ', 'M'}; can be written as: char c [] = {"C program "};

Or remove {} and write it as char c [] = "C program ";

Related Article

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.