one-dimensional arrays1. Definition of one-dimensional arrays
* The form of the definition is: type array name [number of elements]
int a[5];
* [] can only be placed after the name of the array, the following are the wrong wording:
Int[5] A; Error int[] b; Error
* [] The number must be a fixed value, can be a constant (such as 6, 8), a constant expression (such as 3+4, 5*7). You must never use a variable or variable expression to represent the number of elements, and in most cases do not omit the number of elements (except when the array is initialized as a function's formal parameters and arrays)
The following are the correct wording:
int a[5]; INTEGER constant int b[' A ']; Character constant, is actually 65int c[3*4]; Integer constant expression
The following are the wrong wording:
int a[]; No number of elements specified, error int i = 9;int A[i]; Use variables to make the number of elements, error
2. Initialization of one-dimensional arrays
2.1 Class at definition time
int Arr[3] = {1,,2,3};
2.2 Define and assign values first
int arr[3];arr[0] = 1;arr[1] = 2;arr[2] = 3;
2.3 Error procedure
int = {Arr[3];arr};
Because ARR is an address and is a constant, constants cannot be re-assigned, such as 10=8; this is wrong.
3. Storage of one-dimensional arrays
When you define an array, the system allocates a contiguous amount of storage space by array type and number to store the array elements. Note that the array name represents the address of the entire array, which is the starting address of the array.
For example char arr[3] = {' A ', ' B ', ' C '};
In-memory allocations are as follows
Address Array Store Contents
0x1101 Arr[0] A
0x1102 Arr[1] B
0x1103 Arr[2] C
That is, arr's memory address is arr or &arr[0] = 0x1101
C-language one-dimensional arrays