array
1. Creation and initialization of one-dimensional array
Array; A collection of elements of the same type.
How to create an 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
Instance of array creation:
//Code 1 int arr1[10]; //Code 2 int count = 10; int arr2[count];//When can arrays be created normally? //Code 3 char arr3[10]; float arr4[1]; double arr5[20];
Note: before C99, [] can only be constant; After C99, the concept of variable length array is introduced, so that variable naming can be used; For example; int a = 10;int arr[a] = {0};
1.2 initialization of array
Array initialization refers to giving the array some appropriate values while creating the array.
Let's see how the array is initialized:
int arr1[10] = {1,2,3}; int arr2[] = {1,2,3,4}; int arr3[5] = {1,2,3,4,5}; char arr4[3] = {'a',98, 'c'}; char arr5[] = {'a','b','c'}; char arr6[] = "abcdef";
The array is initialized when it is created without specifying the definite size of the array. The number of elements of the array is determined according to initialization.
But for the following code, we should distinguish how to allocate memory.
char arr1[] = "abc"; char arr2[3] = {'a','b','c'};
1.3 use of one-dimensional array
First, an operator is introduced:
[] subscript reference operator, used to access array elements
#include <stdio.h> int main() { int arr[10] = {0};//Incomplete initialization of 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<10; i++)//Write 10 here, okay? { arr[i] = i; } //Output the contents of the array for(i=0; i<10; ++i) { printf("%d ", arr[i]); } return 0; }
Summary:
1. The subscript of the array starts from 0
2. The size of the array can be calculated
1.4 storage of one-dimensional array in memory
Next, let's explore the storage of arrays in memory
Look at the code;
#include <stdio.h> int main() { int arr[10] = {0}; int i = 0; int sz = sizeof(arr)/sizeof(arr[0]); for(i=0; i<sz; ++i) { printf("&arr[%d] = %p\n", i, &arr[i]); } return 0; }
The operation results are as follows:
After careful observation, we know that with the increase of array subscript, the address of the element also increases regularly. It can be concluded that:
Arrays are stored continuously in memory.
2. Creation and initialization of two-dimensional array
2.1 creation of two-dimensional array
int arr[3][4]; char arr[3][5]; double arr[2][4];
2.2 initialization of two-dimensional array
int arr[3][4] = {1,2,3,4}; int arr[3][4] = {{1,2},{4,5}}; int arr[][4] = {{2,3},{4,5}};//If the two-dimensional array is initialized, the row can be omitted and the column cannot be omitted
2.3 use of two-dimensional array
#include <stdio.h> int main() { int arr[3][4] = {0}; int i = 0; for(i=0; i<3; i++) { int j = 0; for(j=0; j<4; j++) { arr[i][j] = i*4+j; } } for(i=0; i<3; i++) { for(j=0; j<4; j++) { printf("%d ", arr[i][j]); } } return 0; }
2.4 storage of two-dimensional array in memory
Like a one-dimensional array, we try to print each element
#include <stdio.h> int main() { int arr[3][4]; int i = 0; for(i=0; i<3; i++) { int j = 0; for(j=0; j<4; j++) { printf("&arr[%d][%d] = %p\n", i, j,&arr[i][j]); } } return 0; }
Take a look at the print results:
Through the print results, we can analyze:
Two dimensional arrays are stored continuously in memory
3. Array out of bounds
The subscript of an array is range limited.
The subscript of the array is specified to start from 0. If there are n elements, the subscript of the last element is n-1;
Therefore, if the lower of the array is less than 0 or greater than n-1, it means that the array is accessed beyond the bounds and beyond the legal space of the array.
C language itself does not check the bounds of array subscripts, and the compiler does not necessarily report errors, but if the compiler does not report errors, it does not mean that the program is correct.
So when programmers write code, they'd better do their own cross-border inspection.
#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
4. Array as function parameter
Often when we write code, we will pass the array as a parameter to the function. For example, we need to implement a bubble sorting algorithm.
4.1 wrong design of bubble sorting
//Method 1: #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; } #include <stdio.h> int main() { int arr[10] = { 1,2,3,4,5 }; printf("%p\n", arr); printf("%p\n", &arr[0]); printf("%d\n", *arr); //Output results return 0; }
Method 1: let's find the problem. Through debugging, we can see the bubble_ The sz inside the sort function is 1 When an array is passed to a function as a parameter, does it not pass the entire array?
Let's move on:
4.2 what is the array name?
#include <stdio.h> int main() { int arr[10] = {1,2,3,4,5}; printf("%p\n", arr); printf("%p\n", &arr[0]); printf("%d\n", *arr); //Output results return 0; }
Conclusion:
The array name is the address of the first element, with two exceptions:
1. sizeof (array name), calculate the size of the whole array. sizeof has a separate array name inside, and the array name represents the whole array.
2. & array name. The address of the array is taken out& Array name, which represents the whole array.
4.3 correct design of bubble sorting
//Method 2 void bubble_sort(int arr[], int sz)//Parameter number of array elements received { //The code is the same as the above function } 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; }
Conclusion: Happy New Year!
Please give me three company!