Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What is the plastic one-dimensional array in C language?
What is the plastic one-dimensional array in C language?
First, the definition of one-dimensional array

Type descriptor array name [constant expression];

Description:

1, the type descriptor is used to define the type of each data element in the array. In any array, the types of data and elements are the same.

2. The name of the array is defined in the array name. The naming rules for array names are the same as those for variable names (naming rules for identifiers). Because in C language, we regard the array as a variable.

3. Constant expressions are enclosed in a pair of brackets []. Note that it must be bracket [], not brace {} or parenthesis (). Constant expressions are used to indicate the number of elements in an array.

4. Constant expressions must be expressions composed of constants or symbolic constants, and there can be no variables. Because in C language, all variables must be defined before use. Once a variable is defined, it is not allowed to make any changes. So when defining array variables, once the elements in the array (also called the size of the array) are determined, it is absolutely not allowed to change the size of the array.

5. Each element in the one-dimensional array is stored in the memory in the order specified by the subscript. As we know, in memory, storage space is represented by bytes, and data can only be stored in sequence. Suppose we define a one-dimensional integer array: int a [5]; Then each element in this array will occupy two bytes. Below we give the array storage method starting from the memory address 1000.

Second, the reference of one-dimensional array.

After defining an array, how to use the elements in the array? C language stipulates that only array elements can be referenced one by one, and not all elements in the array can be referenced at one time.

Reference format of array: array name [subscript]

Description:

The 1. array name indicates which element in the array to reference, and the array must have been defined.

2. Subscript is enclosed in a pair of square brackets [], indicating which element in the array to refer to, which can be a variable expression or a constant expression.

3. In C language, the range of subscripts is from [0, the number of elements minus 1]. Suppose we define an array with n elements (n is a constant), then the range of subscripts is [0, N- 1].

Well, we have a certain understanding of the use of one-dimensional arrays. In the above program, we see that there must be a program that assigns values to the array, which takes up the running time. Can we specify the contents of the array before the program runs? Ok, let's take a look at what we introduced.

Thirdly, the initialization of one-dimensional array.

The initialization operation of the array is to define the contents of the array at the same time, that is, what value each array element takes. This initialization process is completed by the compiler when compiling the source program. Before the program runs, it has specified the value of each element in the array. So when using the array, it is best to initialize it, which can save the running time of the program.

The initialization operation of array can take the following ways: array name of static type descriptor [n] = {value 1, value 2, ... value n };;

Description:

1. Initialization of an array can only be performed when the array is defined.

2.n represents the number of elements contained in the array (it can be a constant expression).

3. The content in braces is the initial value of the array. The value of 1 will be assigned to the 0th element, the value of 2 will be assigned to the 1 th element, and so on. After assigning the initial value, the contents of the array are as follows (taking array A as an example): static int a[N]={ value of 1, value of 2, ... N};

4. The keyword static indicates that a static variable is defined. C language stipulates that only static variables and external variables can be initialized (which will be introduced later). But in Turbo C, variables can be initialized without adding the keyword static.

5. Only the initial value can be assigned to the first part of the array element.

6. If you want to assign all the elements in the array to 0, you can do this: static int a [n] = {0 0,0,0, ............................................................................................................................. (to write n zeros) or write it like this: static int a [n] = {0}; (Write only one 0)

7. When the initial value is assigned to all array elements, the size of the array may not be specified.