<> array

<>1. Creation and initialization of one-dimensional array

（1） Array creation
An array is a collection of elements of the same type .
Array creation method

type_t arr_name [const_n];
//type_t Is the element type of the index group
//const_n Is a constant expression , Used to specify the size of the array *（ Variables cannot be used ）**
This is an incorrect array definition , Because the array size uses variables int count = 10; int arr2[count]; int arr1[10];
// Indicates a size of 10 of int Type array char arr3[10]; // Indicates a size of 10 of char Type array float arr4[1];
// Indicates a size of 1 of float Type array .....（ There are many more , I won't list them one by one ）
（2） Initialization of array
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[] = "abcd";
One dimensional array size can also be omitted

Arrays are accessed using subscripts , Subscript is from 0 start
arr[10]={1,2,3,4,5,6,7,8,9,10};// Size 10 Integer array of arr[0]=1,arr[1]=2,arr[2]=3......arr
[9]=10. Note that , No, arr[10] Such a number , because arr[10] It's crossed the line . We'll talk about it later

The size of the array can be calculated
#include<stdio.h> #include<string.h> int main() { int arr[10]={0}; int sz=
sizeof(arr)/sizeof(arr[0]); printf("%d",sz); return 0; }

<>2. Creation and initialization of two-dimensional array

（1） Creation of two-dimensional array
// Array creation int arr[3][4]; char arr[3][5]; double arr[2][4];
first [ ] The number in represents the row , the second [ ] The number in represents the column

（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}};
be careful ： Two dimensional array, if initialized , Lines can be omitted without writing , However, columns cannot be omitted

（3） Use of two-dimensional arrays
The use of two-dimensional arrays is also through subscripts .
Row and column subscripts are from 0 start
#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++)
{ int j = 0; for(j=0; j<4; j++) { printf("%d ", arr[i][j]); } } return 0; }
The storage of two-dimensional arrays is also continuous

（4） Array out of bounds
The subscript of an array is range limited .

The next stipulation of the array is from 0 Started , If the array has n Elements , The subscript of the last element is n-1. So if the subscript of the array is less than 0, Or greater than n-1, Is the array cross-border access , Access to array legal space exceeded .C The language itself does not check the bounds of array subscripts , The compiler does not necessarily report an error , But the compiler does not report an error , That doesn't mean the program is right , So when programmers write code , You'd better do the cross-border inspection yourself .
#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 be equal to 10 When , Cross border visit } return 0; }
The same goes for two-dimensional arrays , Rows and columns may also be out of bounds

（5） Array as parameter

* sizeof( Array name ), Calculate the size of the entire array ,sizeof Put a separate array name inside , The array name represents the entire number
group .
* & Array name , The address of the array is taken out .& Array name , The array name represents the entire array .
In addition 1,2 Except two cases , All array names represent the address of the first element of the array
Look at the code below #include <stdio.h> void bubble_sort(int arr[]) { int sz = sizeof(arr)/
sizeof(arr[0]); 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);
int sz=sizeof(arr)/sizeof(arr[0]); for(i=0; i<sz; i++) { printf("%d ", arr[i]);
} return 0; } similarly int sz=sizeof(arr)/sizeof(arr[0]); main() In function sz=10; and void
bubble_sort(int arr[]) Inside the function sz=1; because void bubble_sort(int arr[]) Inside arr[]
It only represents setting the address of the first element , It can be understood as just passing one arr[0], therefore sz=sizeof(arr)/sizeof(arr[0]) amount to sz=sizeof(arr[
0])/sizeof(arr[0])=1 and main() Inside the function sizeof(arr) Represents the entire array （ Namely 10 number ）, therefore sz=sizeof(arr)/
sizeof(arr[0])=10;
Correct code
void bubble_sort(int arr[], int sz)// Parameter number of array elements received { int sz = sizeof(arr)/sizeof(
arr[0]); 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}; 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; }

Technology
Daily Recommendation