1. Creation and initialization of one-dimensional array
1.1 creation of array
type_t arr_name [const_n];
//type_t is the element type of the index group
//const_n is a constant expression that specifies the size of the array
//Code 1 int arr1[10]; //Code 2 int count = 10; int arr2[count];//Try to avoid the array length as variable (variable decorated with canst is not good, constant variable) Note: array creation, in C99 Before the standard, [] A constant must be given in the. Variables cannot be used. stay C99 The standard supports variable length numbers ( vs (not supported) The concept of group. //Code 3 char arr3[10]; float arr4[1]; double arr5[20];
1.2 initialization of array
Array initialization refers to giving the contents of the array some reasonable initial values (initialization) while creating the array. When creating an array, if you want to not specify the determined size of the array, you have to initialize it. The number of elements of the array is determined according to the initialization content.
If the value is not initialized enough, it will be followed by 0. If not initialized, the array is full of random values.
Initialization case: char arr1[] = "abc"; char arr2[3] = {'a','b','c'};
Global or static variables are not initialized. The default is 0
One dimensional array 2
Operator: [], subscript refers to operator. It is actually the operator of array access
3. Storage of one-dimensional array in memory
A bit array is stored continuously in memory, and increases from low to high with the increase of array subscript
Arrays are stored continuously in memory.
#include <stdio.h> int main() { int arr[10] = {0};//Variables cannot be placed in the incomplete initialization [] of the array //Calculate the number of elements of the array int sz = sizeof(arr)/sizeof(arr[0]); //Assign a value to the contents of the array. The array is accessed by subscript, which starts from 0. So: int i = 0;//Subscript for(i=0; i<sz; i++) { //assignment arr[i] = i; } //Output the contents of the array for(i=0; i<sz; ++i) { printf("%d ", arr[i]); } return 0; }
&Arr [i] = p + I% p print address (HEX)
4. Creation and initialization of two-dimensional array
5. Use of two-dimensional array
The use of two-dimensional arrays is also through subscripts
#include <stdio.h> int main() { int arr[3][4] = {0}; int i = 0; for(i=0; i<sizeof(arr)/sizeof(arr)[0]; i++) { int j = 0; for(j=0; j<sizeof(arr)[0]/sizeof(arr)[0][0]; j++) { arr[i][j] = i*4+j; } } for(i=0; i<sizeof(arr)/sizeof(arr)[0]; i++) { int j = 0; for(j=0; j<sizeof(arr)[0]/sizeof(arr)[0][0]; j++) { printf("%d ", arr[i][j]); } } return 0; }
6. Storage of two-dimensional array in memory
Two dimensional arrays are also stored continuously in memory
7. Array out of bounds
The subscript of an array is range limited. The lower specification of the array starts from 0. If the array has n elements, the subscript of the last element is n-1. Therefore, if the subscript of the array is less than 0 or greater than n-1, the array is accessed beyond the bounds and beyond the access of the legal space of the array. C language itself does not do the cross-border check of array subscripts, and the compiler does not necessarily report errors. However, if the compiler does not report errors, it does not mean that the program is correct. Therefore, when programmers write code, they'd better do the cross-border check themselves
#include <stdio.h> int main() { int arr[10] = {1,2,3,4,5,6,7,8,9,10}; int i = 0; for(i=0; i<=10; i++) { printf("%d\n", arr[i]);//When i equals 10, the cross-border access } return 0; }
Rows and columns of a two-dimensional array may also be out of bounds
8. Array as function parameter
Pass the array as a parameter to a function, for example: I want to implement a bubble sort
#include <stdio.h> void bubble_sort(int arr[]) { int sz = sizeof(arr)/sizeof(arr[0]);//Is that right? int i = 0; for(i=0; i<sz-1; i++) { int j = 0; for(j=0; j<sz-i-1; j++) { if(arr[j] > arr[j+1]) { int tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } } int main() { int arr[] = {3,1,7,5,8,9,0,2,4,6}; bubble_sort(arr);//Can I sort normally? for(i=0; i<sizeof(arr)/sizeof(arr[0]); i++) { printf("%d ", arr[i]); } return 0; }
You can see the bubble after debugging_ sz inside the sort function is 1. When an array is used as a function parameter, does it not pass the of the entire array?
4.2 what is the array name?
The array name is the address of the first element of the array.
Correct bubbling
void bubble_sort(int arr[], int sz)//Parameter number of array elements received { int i = 0; for(i=0; i<sz-1; i++) { int j = 0; for(j=0; j<sz-i-1; j++) { if(arr[j] > arr[j+1]) { //exchange int tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } } int main() { int arr[] = {3,1,7,5,8,9,0,2,4,6}; int sz = sizeof(arr)/sizeof(arr[0]); bubble_sort(arr, sz);//Can I sort normally? for(i=0; i<sz; i++) { printf("%d ", arr[i]); } return 0; }