1. Creation and initialization of one-dimensional array
1.1 creation of array
An array is a collection of elements of the same type.
How to create an array:
Array element type array name [constant expression];
eg. int arr[5]; char ch[100];
Error prone point in VS compiler: [] should be a constant expression
int n = 5;
int arr[n];(×)
int arr[5];(√)
(actually C99 Variables are not supported before the standard. They can only be constants! C99 The concept of variable length array is added to allow the array size to be a variable, and the compiler is required to support it C99 Standard. VS yes C99 Your support is not good enough)
1.2 initialization of array
When creating, some initial values are called initialization
int arr[5] = { 1, 2, 3, 4, 5 };
int arr[5] = { 1, 2, 3 };// Incomplete initialization. The remaining elements are initialized to 0 by default
int arr[] = { 1, 2, 3 };// An array of undetermined size allocates space according to the initialization contents
char arr1[] = { 'a', 'b', 'c' }; char arr2[] = "abc"; //sizeof find array size printf("%d\n", sizeof(arr1));//arr1 has three elements and the array size is 3 bytes printf("%d\n", sizeof(arr2));//arr2 has four elements and the array size is 4 bytes //strlen finds the length of the string and stops when it encounters' \ 0 ' printf("%d\n", strlen(arr1));//There is no '\ 0' at the end of the array. We can't know where '\ 0' will appear, so the length of arr1 is a random value printf("%d\n", strlen(arr2));//There is' \ 0 'at the end of the array, and there are three elements before it. The length of arr2 is 3
strlen is a library function. Add #include < string before use h>
The length of the string is calculated, and only for the string
It focuses on whether there is \ 0 in the string. It calculates the number of characters before \ 0
sizeof is an operator (operator)
sizeof is used to calculate the memory space occupied by variables. Any type can be used
Only pay attention to the size of space, and don't care whether there is \ 0 in memory
1.3 use of one-dimensional array
The array has a subscript. The subscript of the first element is 0 and increases in turn
int arr[5] = { 1, 2, 3, 4, 5 }; printf("%d", arr[2]);//[] is the access operator in the following table. Here is the number with print subscript 2, and 3 is printed //Print all elements of the array, that is, the elements with subscripts 0, 1, 2, 3 and 4 int i = 0; int sz = sizeof(arr) / sizeof(arr[0]);//40 / 4 find the number of elements and the size of the array for (i = 0; i < sz; i++) { printf("%d ", arr[i]); }
1.4 storage of one-dimensional array in memory
int arr[5] = { 1, 2, 3, 4, 5 }; //Prints the address of each element of the array int i = 0; for (i = 0; i < 5; i++) { printf("&arr[%d] = %p \n",i, &arr[i]); }
The difference between every two addresses is 4
An integer is four bytes
A byte in memory gives an address
conclusion
1. One dimensional arrays are stored continuously in memory
2. As the subscript of the array increases, the address changes from low to high
int arr[5] = { 1, 2, 3, 4, 5 }; int i = 0; int *p = &arr[0]; for (i = 0; i < 5; i++) { printf("%p----- %p \n", &arr[i], p + i); }
You can jump to the ith element address with the first address + i
Therefore, you can use * (p+i) to get the ith element (this has something to do with the pointer to be talked about later. Now let's understand it first)
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];
int arr[3][4];
2.2 initialization of two-dimensional array
int arr[3][4] = {1,2,3,4};//Incomplete initialization, if not enough, add 0 int arr[3][4] = {{1,2},{4,5}};//1 2 0 0 //4 5 0 0 //0 0 0 0 int arr[][4] = {{2,3},{4,5}};//If a two-dimensional array is initialized, rows can be omitted and columns cannot be omitted
2.3 use of two-dimensional array
//Print 2D array int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; int i = 0; for (i = 0; i < 3; i++) { int j = 0; for (j = 0; j < 4; j++) { printf("%d ", arr[i][j]); } }
2.4 storage of two-dimensional array in memory
//Prints the address of each element of the array int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 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]); } }
The storage of two-dimensional arrays seems discontinuous, but in fact, they are stored continuously
3. Array out of bounds
The subscript of an array is scoped.
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 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,
Rows and columns of a two-dimensional array may also be out of bounds.
So when programmers write code, they'd better do cross-border inspection by themselves
#include <stdio.h> int main() { int i = 0; int arr[] = {1,2,3,4,5,6,7,8,9,10}; for(i=0; i<=12; i++)//Here, the array is accessed out of bounds, but is this the main problem? { arr[i] = 0; printf("haha\n"); } return 0; }
Let's publish the answer!
The bug in this code is an endless loop
I'm sorry that the full screen of haha has disturbed your eyes (manual dog head)
Is it hard to imagine? Please read the explanation below with doubts
There are several rules:
- i and arr are local variables
- Local variables are placed on the stack
- The usage habit of memory on the stack area is to use the space at the high address first, and then the space at the local address
- As the subscript of the array increases, the address changes from low to high
The schematic diagram is as follows
"From low to high" and "from high to low" met.
arr [12] = 0; Indirectly changed i, equivalent to i = 0;
In this way, i increases from 0 to 0, and then returns to 0, realizing the situation of dead cycle
As for how to determine the number of bytes between the storage phases of the local variables represented by the blank grid, It depends on the compiler. vs The compiler is empty. What are other compilers like? If you are interested, you can explore it yourself.
4. Array as function parameter
4.1 wrong design of bubble sorting function
The core idea of bubble sorting:
Compare two adjacent elements and exchange them if necessary
#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++)//sz-1 bubble sort { 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}; bubble_sort(arr);//Can I sort normally? for(i=0; i<sizeof(arr)/sizeof(arr[0]); i++) { printf("%d ", arr[i]); } return 0; }
What is array name 2.4?
The essence of array parameter passing is not to pass the entire array, but the address of the first element
bubble_ sizeof(arr) in the sort function calculates the size of the pointer, resulting in an error
Arr is essentially the address of the first element. When receiving an array, you can also use int *arr instead of int arr []
Special circumstances: 1& arr
2. 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
4.3 correct design of bubble sorting function
void bubble_sort(int arr[], int sz)//Parameter number of array elements received { //The code is the same as the above function int i = 0; for(i=0; i<sz-1; i++)//sz-1 bubble sort { 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; }
5. Data examples
5.1 application example of array 1: Sanzi chess
My other blogs have written:
tic-tac-toe .
5.2 application example of array 2: minesweeping game
My other blogs have written:
The Minesweeper game.
Let's apply what we have learned and play!