Array (super detailed explanation)

1. One-dimensional array

1.1 Creation of one-dimensional array

An array is a collection of elements of the same type (an array represents a set of data of the same type)

How the array is created:

type_t arr_name [const_n];
//type_t refers to the element type of the array
//const_n is a constant expression used to specify the size of the array

int arr[5];
int arr2[3 + 2];
char arr3[8];

insert image description here

  • For array creation, before the C99 standard, a constant must be given in [ ], and variables cannot be used.
  • In the C99 standard, the concept of variable-length arrays was added
  • The size of the array can be specified using variables, but once the size of the array is determined, it cannot be changed
  • Variable-length arrays cannot be initialized
  • Variable-length arrays in C99 are not supported in VS2019 and 2022 environments

1.2 Array initialization

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

Look at the code:

	int arr1[10] = {
    
     1,2,3 };//不完全初始化,剩余元素默认初始化为0
	int arr2[5] = {
    
     1,2,3,4,5 };//完全初始化
	int arr3[3] = {
    
     'a',98,'c' };//98为ASCII值
	int arr4[0] = {
    
     0 }//错误数组大小不能为零
	char arr6[] = "abcdef";//字符串初始化含\0
	char arr7[] = {
    
     'a','b','c' };//不含、0

1.3 Use of one-dimensional arrays

For the use of arrays, we introduced an operator before: [ ], the subscript reference operator. It is actually an operator for array access.

Let's look at the code:

int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	              //0 1 2 3 4 5 6 7 8 9
	printf("%d", arr[7]);
	int sz = sizeof(arr) / sizeof(arr[0]);//计算数组大小
	int i = 0;
	printf("\n");
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);//表示从1~10
	}
	printf("\n");
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", 10-i);//表示从10~1
	}
	return 0;
}

Running results:
insert image description here
summary:

  1. Arrays are accessed using subscripts, which start at 0.
  2. The size of the array can be calculated.

1.4 Storage of one-dimensional arrays in memory

insert image description here
insert image description here
Note:

  • Arrays are stored contiguously in memory
  • As the subscript increases, the address changes from low to high
    insert image description here

2. 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 arr1[3][5] = {
    
     {
    
    1,2,3,4,5},{
    
    1,2,3,4,5} ,{
    
    1,2,3,4,5} };//完全初始化
	int arr2[3][5] = {
    
     0 };//不完全初始化
	int arr3[][5] = {
    
     {
    
    1,2,3},{
    
    5,5},{
    
    6,7,8,9} };
	int arr4[][] = {
    
     0 };//二维数组如果有初始化,行可以省略,列不能省略
	int arr5[0][5] = {
    
     0 };//数组的大小必须大于零

2.3 Use of two-dimensional arrays

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

Look at the code:

int main()
{
    
    
	int arr[3][5] = {
    
     {
    
    1,2,3},{
    
    5,5},{
    
    6,7,8,9} };
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
    
    
		for (j = 0; j < 5; j++)
			printf("%d ", arr[i][j]);
		printf("\n");
	}
	return 0;
}

operation result:
insert image description here

2.4 Storage of two-dimensional arrays in memory

Like a 1D array, here we try to print each element of a 2D array
insert image description here
insert image description here
insert image description here

3. Array out of bounds

Array subscripts are limited in scope.
The subscript of the array starts from 0. 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, it means that the array is accessed out of bounds, and the access exceeds 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 necessarily report an error, but if the compiler does not report an error, it does not mean that the program is correct. Therefore, when programmers write code, it is best to do the out-of-bounds check by themselves.

insert image description here

Two-dimensional arrays may also have out-of-bounds

4. Arrays as function parameters

Often when we write code, we will pass an array as a parameter to a function, for example: I want to implement a bubble sort (here we will talk about algorithm thinking) function

To sort an integer array, then we will use the function like this:

4.1 Bubble sort function error design

insert image description here

4.2 What is the name of the array

The array name is the address, generally speaking: the array name is the address of the first element of the array,
insert image description here
but there are two exceptions
1, sizeof (array name), the array name here represents the entire array, the calculation is the size of the entire array, the unit is byte 2, & array name, 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 address of the first element of insert image description here
the
insert image description here
array

4.3 Correct Design of Bubble Sort Function

//void Bubble_sort(int *arr, int sz)
void Bubble_sort(int arr[],int sz)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < sz; i++)
	{
    
    
		for (j = 0; j < sz - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
int main()
{
    
    
	int arr[10] = {
    
     10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	int i;
	int sz = sizeof(arr) / sizeof(arr[0]);
	Bubble_sort(arr,sz);//arr首元素地址代表数组
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

operation result:
insert image description here

.

Guess you like

Origin blog.csdn.net/2201_75642960/article/details/131897927