One dimensional array and two-dimensional array

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:
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:
int main()
{
   /* int a = 1;
	int b = 2;
	int c = 3;*/
	//Storage 1 ~ 100, array required
	//An array is a collection of elements of the same type
	int arr[100]={1,2,3,4,5,6,7,8,9,10};
	int arr2[100] = { 0 };

	return 0;
}
Note: Array creation, in C99 Before the standard, [] I want to give one constant You can't use variables. stay C99 The standard supports the concept of variable length arrays.

1.2 initialization of array

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

    int arr1[10] = { 1,2,3,4 };//Incomplete initialization
	int arr2[] = { 1,2,3,4 };//Specifies the size of the array based on the contents of the root array
	char ch1[] = { 'a','b','c' };
	char ch2[] = { 'a',98,'c' };//The two are the same because the ascii value of b is equal to 98

When creating an array, if you want to not specify the determined size of the array, you have to initialize it. The number of elements of the array is determined according to the initialization content. But for the following code, we should distinguish how to allocate memory.

    char arr1[] = "abc";//a b c \0
	char arr2[] = { 'a','b','c' };//a b c

1.3 use of one-dimensional array

For the use of arrays, we introduced an operator earlier: [] , subscript reference operator. It is actually the operator of array access.
#include <stdio.h>
int main()
{
	int arr[100] = { 1,2,3,4,5,6 };
	//              0 1 2 3 4 5
	//Write code to assign a value of 1 ~ 100
	//printf("%d\n", sizeof(arr));//400
	//printf("%d\n", sizeof(arr[0]));//4
	int sz = sizeof(arr) / sizeof(arr[0]);//How to calculate the number of array elements

	int i = 0;
	//assignment
	for (i = 0; i < sz; i++)
	{
		arr[i] = i + 1;
	}

	//Print
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}
summary :
1. Arrays are accessed using subscripts from 0 Start.
2. The size of the array can be calculated.
int arr[10];
int sz = sizeof(arr)/sizeof(arr[0]);

1.4 storage of one-dimensional array in memory

//Storage of one-dimensional array in memory
#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//Prints the address of each element of the array
	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 output results are as follows:

                                         

Carefully observe the output results. We know that with the increase of array subscript, the address of the element is also increasing regularly. It can be concluded that arrays are stored continuously in memory.

//Storage of one-dimensional array in memory
#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//Prints the address of each element of the array
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	int* p = &arr[0];

	for (i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));
	}

	/*for (i = 0; i < sz; i++)
	{
		printf("&arr[%d]=%p<====> %p\n", i, &arr[i],p+i);
	}*/

	return 0;
}
//%p-print address (hexadecimal)
//%d - print integer (decimal)
//Arrays can be accessed in the form of subscripts or pointers

2. Creation and initialization of two-dimensional array

2.1 creation of two-dimensional array

//Array creation
int arr[3][4];
char arr[3][5];
double arr[2][4];

2.2 initialization of two-dimensional array

//Array initialization
int arr[3][4] = {1,2,3,4};//Incomplete initialization
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

The use of two-dimensional array is also through subscript. There are two methods:
#include <stdio.h>
int main()
{
	int arr2[][5] = { {1,2},{4,5},{5,6} };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 5; j++)
		{
			printf("%d ", arr2[i][j]);
		}
		printf("\n");
	}
	return 0;
}
#include <stdio.h>
int main()
{
	int arr2[][5] = { {1,2},{4,5},{5,6} };
	int i = 0;
	for (i = 0; i < sizeof(arr2)/sizeof(arr2[0]); i++)
	{
		int j = 0;
		for (j = 0; j < sizeof(arr2[0])/sizeof(arr2[0][0]); j++)
		{
			printf("%d ", arr2[i][j]);
		}
		printf("\n");
	}
	return 0;
}

2.4 storage of two-dimensional array in memory

#include <stdio.h>
int main()
{
	int arr[3][5] = { {1,2},{4,5},{5,6} };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 5; j++)
		{
			printf("&arr[%d][%d]=%p\n", i, j, &arr[i][j]);
		}
	}
	return 0;
}
The output is as follows:
Through the results, we can analyze that in fact, two-dimensional arrays are also stored continuously in memory.

3. Array out of bounds

The subscript range of the array is limited.
The next stipulation of the array is from 0 First, if the array has n 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 , that is, the access of the array is out of bounds, which exceeds the access of the legal space of the array.
C The 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 cross-border inspection by themselves.
#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 two-dimensional arrays 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 a function. For example, I want to implement a bubble sorting (here we will talk about the idea of algorithm) function to sort an integer array.

4.1 wrong design of bubble sorting function

//Method 1
#include <stdio.h>
void bubble_sort(int arr[])//Array receiving
{
	int sz = sizeof(arr) / sizeof(arr[0]);
	//Number of trips
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		//Each bubble sorting process
		int j = 0;
		for (j = 0; j <sz-1-i ; 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[] = { 9,8,7,6,5,4,3,2,1,0 };
	//Write a bubble sort function to sort the contents of the arr array
	// arr - the address of the first element of the
	// &arr[0]
	//Write a bubble sort function to sort the contents of the arr array
	//Bubble sort: compare two adjacent elements in the array. If they do not meet the conditions, they will be exchanged. The sort is ascending
	bubble_sort(arr);
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}
method 1 , if something goes wrong, let's find the problem and see it after debugging bubble_sort Function internal sz , yes 1 .
When an array is used as a function parameter, does it not pass the of the entire array?

4.2 what is the array name?

#include <stdio.h>
int main()
{
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    printf("%p\n", arr);
    printf("%p\n", arr + 1);

    printf("%p\n", &arr[0]);
    printf("%p\n", &arr[0] + 1);

    printf("%p\n", &arr);
    printf("%p\n", &arr + 1);
    //Output results
    return 0;
}
The array name is the address of the first element of the array. (with two exceptions)
If the array name is the address of the first element:
 
Supplement:
1. Sizeof (array name). The array name is not the address of the first element. The array name represents the whole array and calculates the size of the whole array
2. & array name. The array name is not the address of the first element of the array. The array name represents the whole array, and the address of the whole array is taken out
In addition 1,2 In both cases, all array names represent the address of the first element of the array.

4.3 correct design of bubble sorting function

#include <stdio.h>
void bubble_sort(int arr[],int sz)//Array receiving
{
	//Number of trips
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int flag = 1;//The assumption has been ordered
		//Each bubble sorting process
		int j = 0;
		for (j = 0; j <sz-1-i ; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				flag = 0;
				//exchange
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
		if (1 == flag)
		{
			break;
		}
	}

}
int main()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
	//Write a bubble sort function to sort the contents of the arr array
	// arr - the address of the first element of the
	// &arr[0]
	//Write a bubble sort function to sort the contents of the arr array
	//Bubble sort: compare two adjacent elements in the array. If they do not meet the conditions, they will be exchanged. The sort is ascending
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr,sz);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

Keywords: C Algorithm

Added by MrJW on Fri, 04 Feb 2022 12:27:51 +0200