Elementary C Language - Array

"Youth has no radicals, and you are a Huazhang!" Today we will continue to learn about array-related knowledge points.

1. Creation and initialization of one-dimensional array

What is an array?数组是一组相同类型元素的集合。

1.1 Array creation

//数组的创建
//type_t arr_name [const_n]
//type_t 是数组的元素类型
//arr_name是数组名
//const_n是一个常量表达式,用来指定数组的大小
//数组创建的示例:
//C99之前数组只能是常量指定大小,C99之后引用了变长数组的概念,数组的大小可以使用变量指定的
//但是VS2019不支持C99的变长数组的
#include <stdio.h>
int main()
{
    
    
	int arr1[10];
	int arr2[3 + 2];
	char arr3[8];
	return 0;
}

1.2 Array initialization

The initialization of the array refers to: giving some reasonable initial values ​​(initialization) to the contents of the array while creating the array.

#include <stdio.h>
int main()
{
    
    
	int arr1[10] = {
    
     1,2,3 };//不完全初始化,剩余元素初始化为0
	int arr2[4] = {
    
     1,2,3,4 };//完全初始化
	char arr3[] = {
    
     'a','b','c' };//数组在创建的时候如果想不指定数组的确定的大小就得初始化。数组的元素个数根据初始化的内容来确定。
	char arr4[] = {
    
     'a',98,'c' };
	char arr5[] = {
    
     "abcdefgh" };
	return 0;
}

insert image description here
insert image description here
insert image description here
Here, we need to distinguish between initializing arrays with characters and initializing arrays with strings. Through monitoring, we can find that '\0'there are .
insert image description here
insert image description here

1.3 Use of one-dimensional arrays

For the use of arrays, we introduced an operator before: [],下标引用操作符。it is actually an operator for array access.

//一维数组的使用
#include <stdio.h>
int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	//            0,1,2,3,4,5,6,7,8,9
	//注意:数组的下标是从0开始的
	printf("%d\n", arr[6]);//相应的下标就会对应相应的数组元素
	return 0;
}

insert image description here
So what should we do if we want to print out all the elements of the array? Do you want to use 10 printf语句? Here, it is obviously impossible. At this time, we have to think of using it for循环to solve the problem.

#include <stdio.h>
int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	//            0,1,2,3,4,5,6,7,8,9
	int sz=sizeof(arr)/sizeof(arr[0])-1;
	int i = 0;//做下标
	for (i = 0; i <= sz; i++)//数组的元素是通过下标来访问的
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

insert image description here

Just now we just used subscripts to print the elements of the array. If we want to change the value of the elements of the array, what should we do?
For example, if we want to change the array elements now 10 9 8 7 6 5 4 3 2 1, how should we change the code?

#include <stdio.h>
int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	//            0,1,2,3,4,5,6,7,8,9
	int sz = sizeof(arr) / sizeof(arr[0])-1;
	int i = 0;
	for (i = 0; i <= sz; i++)
	{
    
    
		arr[i] = 10 - i;
		printf("%d ", arr[i]);
	}
	return 0;
}

insert image description here

1.4 Storage of one-dimensional arrays in memory

If you want to know where the arrays are stored in memory, you can easily observe them by printing out their addresses.

#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;
}

insert image description here
Note: Through careful observation, we know that with the growth of the array subscript, the address of the element is also increasing regularly. Therefore, as long as数组在内存中是连续存放的。
we know the first address of the array, we can access all the elements of the array by traversing up.
insert image description here

2. Creation and initialization of two-dimensional array

2.1 Creation of two-dimensional array

char arr1[3][5]//第一个表示行第二个表示列
int arr2[3][5]int arr3[2][3]

2.2 Initialization of two-dimensional array

//二维数组的初始化
#include <stdio.h>
int main()
{
    
    
	char arr1[3][5] = {
    
     0 };
	int arr2[3][5] = {
    
     {
    
    1,2,3,4,5},{
    
    6,7,8,9, 8},{
    
    1,2,3,4,5}};
	int arr3[2][3] = {
    
     {
    
    1,2},{
    
    3,4} };
	int arr4[][4] = {
    
     {
    
    1,2},{
    
    1,2,3,4} };//二维数组如果有初始化,行可以省略,列不能省略
	return 0;
}

insert image description here

2.3 Use of two-dimensional arrays

The use of two-dimensional arrays is also accessed through subscripts.

int arr2[3][5] = {
    
     {
    
    1,2,3,4,5},{
    
    6,7,8,9, 8},{
    
    1,2,3,4,5}};

For such a two-dimensional array, we assume that its arrangement is like this:
insert image description here
then we can see that the corresponding elements can be accessed through the subscripts of the corresponding row numbers and column numbers. So, what should we do if we want to print out all the elements of a two-dimensional array?

#include <stdio.h>
int main()
{
    
    
	int arr[2][3] = {
    
     {
    
    1},{
    
    2,3} };
	int i = 0;
	for (i = 0; i < 2; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < 3; j++)
		{
    
    
			printf("%d ", arr[i][j]);
		}
	}
	return 0;
}

insert image description here

2.4 Storage of two-dimensional arrays in memory

Like a 1D array, we can try to print the address of each element of a 2D array.

#include <stdio.h>
int main()
{
    
    
	int arr[2][3] = {
    
     {
    
    1},{
    
    2,3} };
	int i = 0;
	for (i = 0; i < 2; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < 3; j++)
		{
    
    
			printf("&arr[%d][%d] = %p\n",i,j,&arr[i][j]);
		}
	}
	return 0;
}

insert image description here
Note: Through careful observation, we know that with the growth of the array subscript, the address of the element is also increasing regularly. Therefore,二维数组在内存中是连续存放的。

Therefore, the storage of a two-dimensional array in memory should be like this: a two-dimensional array can also be understood as an array of one-dimensional arrays. a[0]The sum shown in the figure below a[1]can be understood as the names of two one-dimensional arrays divided into two-dimensional arrays.
insert image description here

3. Array out of bounds

The subscript of the array is limited in scope. The subscript of the array is specified from the 0beginning. If the array has n elements, the subscript of the last element is n-1. So the subscript of the array 如果小于0,或者大于n-1is 数组越界访问the access beyond the legal space of the array. . The C language itself does not perform an out-of-bounds check for array subscripts, and the compiler may not report an error, but the fact that the compiler does not report an error does not mean that the program is correct, so when programmers write code, it is best to do the out-of-bounds check by themselves.

#include <stdio.h>
int main()
{
    
    
	int arr[2][3] = {
    
     {
    
    1},{
    
    2,3} };
	int i = 0;
	for (i = 0; i < 2; i++)
	{
    
    
		int j = 0;
		for (j = 0; j <= 3; j++)//我们发现这里列存在越界行为,但是编译器并没有报错
		{
    
    
			printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
		}
	}
	return 0;
}

insert image description here
What is the correct way to write it?

#include <stdio.h>
int main()
{
    
    
	int arr[2][3] = {
    
     {
    
    1},{
    
    2,3} };
	int i = 0;
	for (i = 0; i < 2; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < 3; j++)
		{
    
    
			printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
		}
	}
	return 0;
}

insert image description here
Note: The rows and columns of the two-dimensional array may also be out of bounds.

4. Arrays as function parameters

Often when we write code, we pass an array as a parameter to a function. For example: I want to implement a bubble sort.

The idea of ​​​​bubble sorting: compare two adjacent elements.
Assuming we have it here now 10 9 8 7 6 5 4 3 2 1这十个数, what should we do if we want to achieve ascending order through bubble sort?
insert image description here
It is not difficult to find that if we want to arrange ten numbers, we need to bubble up to nine times, so let's demonstrate this process with code!

//冒泡排序
#include <stdio.h>
int main()
{
    
    
	int arr1[] = {
    
     10,9,8,7,6,5,4,3,2,1 };
	int i = 0;
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	for (i = 0; i < sz - 1; i++)
	{
    
    
		int j = 0;
		for (j = 0; j <sz-i-1 ; j++)
		{
    
    
			if(arr1[j]>arr1[j+1])
			{
    
    
				int t = arr1[j];
				arr1[j] = arr1[j + 1];
				arr1[j + 1] = t;
			}
		}
	}
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr1[i]);
	}
	return 0;
}

insert image description here
We also learned about functions earlier, can we design a bubble sorting function here?

#include <stdio.h>
void bubble_sort(int arr[])
{
    
    
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[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 t = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = t;
			}
		}
	}
}
int main()
{
    
    
	int arr1[] = {
    
     10,9,8,7,6,5,4,3,2,1 };
	bubble_sort(arr1);
	int i = 0;
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr1[i]);
	}
	return 0;
}

Maybe the code we thought of at the beginning is like this, so is it right to write it this way?
insert image description here
We found that this did not help us achieve the ascending function! So, what is the problem?
insert image description here
Through debugging, we found that there is a problem in calculating the number of array elements in the bubble function sz. Why is this? This involves using an array as a parameter of a function. What exactly is passed? Is the entire array being passed? Here, we need to get to know the array name!

4.2 What is the name of the array?

usually,数组名是数组首元素的地址。

#include <stdio.h>
int main()
{
    
    
	int arr[10] = {
    
     0 };
	printf("%p\n", arr);
	printf("%p\n", &arr[0]);
	return 0;
}

insert image description here

Note: Generally speaking, the array name is the first address of the array. However, there are two exceptions:
1. sizeof(数组名), the array name here means the entire array, and the calculation is the size of the entire array, and the unit is byte.
2. &数组名, the array name here represents the entire array, and the address of the entire array is taken out.
In addition, all encountered array names are the first address of the array. </font

Similarly, after understanding the above knowledge, we can find the problem of the bubble sort just now:

//数组名作为函数参数(错误)
#include <stdio.h>
//应该使用指针来接收
void bubble_sort(int arr[])//本质是指针
{
    
    
	int i = 0;
	//数组名作为函数的参数传过去的是数组的首地址,没办法求数组元素个数。
	int sz = sizeof(arr) / sizeof(arr[0]);//4/4=1
	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 t = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = t;
			}
		}
	}
}
int main()
{
    
    
	int arr1[] = {
    
     10,9,8,7,6,5,4,3,2,1 };
	bubble_sort(arr1);//这里的arr1不是两种特殊的情况,就是数组首元素的地址
	int i = 0;
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr1[i]);
	}
	return 0;
}

4.3 Correct Design of Bubble Sort Function

When an array is passed as a parameter, in fact, only the address of the first element of the array is passed. So even if the parameter part of the function is written in the form of an array: int arr[]表示的依然是一个指针:int *arr.Therefore, here we consider to find it directly inside the main function sz, and use it directly in the function of the bubble sort.

//数组名作为函数参数(正确)
#include <stdio.h>
//void bubble_sort(int *arr,int sz)
void bubble_sort(int arr[],int sz)
{
    
    
	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 t = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = t;
			}
		}
	}
}

int main()
{
    
    
	int arr1[] = {
    
     10,9,8,7,6,5,4,3,2,1 };
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	bubble_sort(arr1,sz);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr1[i]);
	}
	return 0;
}

insert image description here
We found that this solved the problem just now!

Well, this is the end of the knowledge points about arrays, and we will continue to update the knowledge points related to C language in the future. Welcome everyone to continue to like, follow and comment! Everyone support the group!

Guess you like

Origin blog.csdn.net/qq_73121173/article/details/132062508