Array
A: 1, what is an array?
A set of several ordered data sets of the same type
2, the noun of the array:
1) array name: A set of several ordered data sets of the same type with a table ()
2) array elements: each element that makes up an array
3) Subscript: The array is ordered and subscript is ordered to identify the position of each element in the array
4) Dimensions of the array: number of elements subscript
5) The length of the array: the number of elements in the collection
3. Classification:
1) Categorized by content: Numeric Array (integer, real), character array, pointer array, constructed array
2) Classification by dimension: one-dimensional array, two-dimensional array, multidimensional array
Two: 1, one-dimensional array definition?
Format:
Data type array name [length];1+2 4+5
int a[10];
Defines an integral type of array a
Another description: Defines a length of 10, each element can only hold an array of int data array name is a
int means that each element in the array must be of type int
Attention:
1) A is the name of the array, and a is required to follow the naming conventions of the identifiers
(1) Letters, numbers, _, dollar sign, cannot start with a number, cannot be the same name as the keyword, strictly case-sensitive
2) array length cannot use variables,
int n=5; int arr[n]; This type of C specification is not possible, but Xcode can
Reason: Cannot use the definition array time length with the variable initialization simultaneously, if defines the array the time length uses the variable, compiles does not run, the compiler sees the variable, simultaneously initializes the word, the compiler does not determine whether exceeds the array length range
3) array length can be defined using macros
#define N 5; int arr2[n+3];//This is a possible notation.
4) array name cannot be the same as other variables
int A;
int a[10]; error
5) Multiple arrays can be defined at once
int a[10],b1[3]; Represents the definition of two integer array, the first array name A, can hold 10 int type data, the second array B1 can hold 3 int type data
6) Once the array is defined, only data of the defined type can be stored
int a[10]; Only data of int type can be stored
7) define the array at the same time as the variable can be defined
int a,b,arr[10];
Three: 1, the initialization of the array
Initialization of variables, assigning initial values to variables
Initializes the array to initialize each element in the array;
2. How to initialize an array
Array if the definition is complete, there is no initialization, at this time, the value of the element is a random number (garbage value)
Format initialized: int array name [length]={value 1, value 2,.....}
1) Define the simultaneous initialization of int a=10;
(1) Full initialization
int a[5]={1,2,3,4,55};
Note: The number of initialization numbers can be less than the length, but not greater than the length
Does not specify a full initialization of the array length
int a3[]={1,2,3}; Defines an array whose length is determined by the number of elements initialized
Misunderstanding:
a3={1,2,3}; Error
a3[]={1,3,4}; Error
(2) Partial initialization
int a1[5]={1}; Initialize 1 to the first element as a priority
Specifies the partial initialization of the element.
int a2[5]={[2]=1,[4]=100};
Initializes the third element to 1 to initialize the 5th element to 100
2) Define first, then initialize
Misunderstanding:
int a1[5];
a1[]={1,2,3}; of the wrong
a1={1,2,3}; of the wrong
Correct usage: Each element is initialized individually
A1[0] = 10;
A1[1] = 100;
A1[2] = 1000;
A1[3] = 10000;
A1[4] = 1;
A1[5] = 0; of the wrong
3. References to arrays
The reference to the array, in fact, is to get the contents stored in the array
Each element in an array can be accessed through an array subscript
If the array length is n, the maximum value of the subscript is n-1, the minimum is 0
If the subscript maximum value of an array is n, the array has a minimum of n+1 elements
Four: 1, the array storage method
4 bytes of difference per address
int a[3]={13,34,438};
Address of the first address of the array = a[0] = a
So, the array name is actually the first address of the array.
2, the length of the array calculation method
The array of words that are occupied in memory
Number of knots: sizeof (int) *n number = sizeof (array name)
3. Access the subscript of the array, exceeding the maximum subscript
About the wrong object, cross-border program will give a warning, but will not error, we must pay attention to the cross-border
Five: The relationship between the array and the function:
1) The elements of the array as parameters of the function (passed the value of the variable, the function ends the memory release)
The parameters of the function are passed arguments to the calling function (must be the same data type)
Note: The array element acts as a parameter to the function, which is the value passed
2) array name as the parameter of the function (the address is the pointer)
So you can modify the elements in the original array
Attention:
1) array name as a function parameter, is the address of the pass, in the function can directly modify the contents of the array
2) using the array name as the function parameter, will lose the array length, therefore uses the array name as the function parameter, must pass the array name simultaneously, as far as possible the array length also passes over
The function calls the array address of the simultaneous array length of 8, because it asks for the size that the pointer occupies in the in-memory address. In the main function, the sizeof (array name) asks for an array of memory size (not quite clear)
3) The length of the array defined in the function parameters, and the length of the real parameter group as much as possible, otherwise there may be problems
4) Normally the array length of the formal parameter is unspecified, and its length is determined by the argument.
5) The type of the form parameter group of the function, and the type of the real parameter group to be one to
C-language arrays