Garbage collection array in C Language
--- Restore content start ---
Learning c language knows that arrays and pointers have a special position in c. It is also a must-have knowledge that will benefit you from learning it.
I. Array
1. array: composed of a series of identical elements. It is continuously stored in the memory.
2. array declaration:
Format: type array name [number of elements];
Example: int myarray [10];
Note: Before c99, the number of elements is variable.
Example:
Int n = 10;
Int myarray [n]; // not allowed before C99 (Initialization is not allowed for this declaration method)
3. array initialization:
- Int myarray [] = {1, 2, 3, 4, 5}; // correct. In this case, the number of elements can be omitted.
- Int myarray [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // In general, "," is used between the element and the element.
- Int myarray [10] = {1, 2, 3}; // only initialize the first three elements.
- Int myarray [10] = {[4] = 4, [6] = 6}; // The first 5th elements are initialized, and the other elements are all 0.
- Int n = 10; int myarray [n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // error. After C99, variables can be used as the number of elements in the array, but such declarations cannot be initialized.
If we do not initialize the array after it is defined, what is the value of the array element?
When an array is defined, it just draws an area and uses the array name to store array elements. However, when defining an array, the data in this area is not changed. That is to say, the value in this area is uncertain.
4. array assignment
- Int myarray [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
- Int oarray [10];
- Oarray = myarray; // error. c does not support assigning values to an array as a whole.
- Oarray [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // error. c does not support a value enclosed by curly brackets.
5. array Boundary
When using arrays, note that the array index cannot exceed the array boundary. That is to say, the array index should be valid for the array.
--- Restore content end ---