1. Arrays
A basic data type, such as an int double, is used to store data, and only one data is stored. The array is used to store a set of data as its name implies.
The attention point of the array:
1. Once the type of the array is determined, then the stored data must be of this type of data
int array[10]; Only data of type int can be stored
2. The length of the array is fixed, and the length is the number of data stored. When we create an array, we specify the length of the array, which cannot be changed once specified.
3. Rigorous array out of bounds!
2. Array definition Format
Data type array name [number of elements];
int array[3]; We have defined an array of type int, which can store only 3 data of type Int. The length of an array cannot be minimized by not initializing arrays
3. Array in memory storage
int array[3];//We define an array of type int that holds 3 data,//Then we then request a contiguous space for the specified number of bytes to store the data (under the 64-bit compiler, the int type is 4 bytes)//system based on index (index increments from 0) To store the data.//variables are stored in complementary binary form, low-level storage in the lower-byte, high-bit stored in the highest byte. And our array name is the low-byte address of the element indexed to 0. (The array name is similar to a pointer, but not a pointer)
4. Initialization of arrays
int array[2] = {1,2};//array full initialization int arrayone[2] = {3}//partial initialization of the array, when partially initialized, the system defaults to the uninitialized part assigned to 0
5. Iterating the array
int main (int argc, const char * argv[]) { int array[10] = {2,3,1,7,2,5,1,9,7,5}; int length = sizeof (array)/sizeof (array[0]);//The length of the array is the number of bytes in the array/number of bytes in the array element for (int i = 0; i < length; i++) {
printf ("%d\n", Array[i]); } return 0;}
6, arrays and functions
Note: 1. When an element in an array is passed as a parameter of a function, the value is passed.
2. When the array name is passed as an argument to a function, it is an address pass (the array name is the low-byte address of the No. 0 element), and the value of the argument can be modified (similar to the pointer) when passed through the address
3. If the array name is passed as a function parameter, be sure to pass in the length of the array. The length of the actual array cannot be computed through an array of formal parameters.
void Printfarray (int array[],int length); int main (int argc, const char * argv[]) { int arr[3] = {1, 2, 5}; int length = sizeof (arr)/sizeof (arr[0]); Printfarray (arr, length); return 0;} void Printfarray (int array[],int length) {for (int i = 0; i < length; i + +) { printf ("%d\n", Array[i]);
}}
7. Tips for array indexing
Tip 1:
Required to input 3 0~9 from the keyboard, output other than the input 0-9 of the number int array[10] = {0};//initialize the array to 0 int num =-1; for (int i = 0; i < 3; i + +) { // Enter 3 digits printf ("Please enter%d number \ n", i + 1); scanf ("%d", &num); With the number of dead 0-9 array[num] = 1; To enter a number as an array index, if you enter a number, the number is indexed, the data on the corresponding index is assigned a value of 1 } for (int j = 0; J < J + +) { if (array[j]! = 1)
//iterates through the array, outputting an index with an element value of not 1 { printf ("%d\n", j);} }
Tip 2:
Requires a 3 0~9 number from the keyboard, sorted output //0 1 2 3 4 5 index //0 0 2 1 0 0 element value int array[10] = {0};//initialize array to 0 int num =-1; for (int i = 0; i < 3; i + +) { printf ("Please enter the number of%d \ n", i + 1); scanf ("%d", &num); Array[num] + = 1;//counter thought, if the input number, the second number is indexed, the corresponding index will be the value of +1; } for (int i = 0; i <; i + +) { for (int j = 0; J < Array[i]; j + +)//counter for repeating input data { printf ("%d\n" , i); } }
Array of C languages