[First knowledge of C language] array

1. One-dimensional array

1. What is an array

1. What is an array?
An array is a collection of elements of the same type . For example, we want to define 10 integer variables (1~10). Before learning about arrays, we define a number with a variable.

int x1 = 1;
int x2 = 2;
int x3= 3;
int x4 = 4;
.....
int x10 = 10;

You will find that if we define a variable and create a variable, it will be terrible when we want to define hundreds or even more variables, and you will find that these types are the same, so we The concept of an array is introduced. Still taking the above as an example, if we want to define ten integer variables, we can write like this

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

1. Creation and initialization of one-dimensional array

1. How to create an array

element type array name [constant expression];
example:

int arr [10];

int count  = 10;
int arr_2[count];//用变量来表示元素个素的是变长数组
//注意事项:数组创建时,在C99标准之气,[]中要给一个变量才可以,不能使用变量,只有在C99标准下支持了变长数组的概念。
float arr_3[20];
double arr_4[50];
char arr_5[100];

2. How to initialize the array

Array initialization refers to giving some reasonable initial values ​​to the contents of the array while creating the array.
1. Incomplete initialization:
the initialized value is less than the number of defined elements, and the uninitialized ones will be automatically filled with 0

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

	return 0;
}

insert image description here
2. Complete initialization: the number of values ​​in the initial words is equal to the defined length

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

	return 0;
}

insert image description here

3. Use sizeof to find the length of the array

The sizeo statement is used to calculate the byte length of the array:
sizeof (array name) = byte length of the entire array;
sizeof (array name [any number that matches the length of the array ] ) = a byte length of the array;
sizeof (array name)/sizeof(array name[arbitrary number that matches the length of the array] = length of the array

for example:

#include<stdio.h>

int main()
{
    
    
	int arr[] = {
    
    0,1,2,3,4,5,6,7,8,9};
	printf("%d\n", sizeof(arr));
	printf("%d\n", sizeof(arr[0]));

	return 0;
}

insert image description here

2. The use of one-dimensional array

For the use of arrays, we introduced an operator before: [ ], the subscript reference operator. He is actually an operator used for array access. Note that subscripts start at 0;
for example

#include<stdio.h>
int main()
{
    
    
	int arr[5] = {
    
     12,34,56,78,99 };
	printf("%d\n", arr[0]);
	printf("%d\n", arr[4]);

	return 0;
}

insert image description here

I want to access the first element in the arr value, then my subscript is arr[ 0 ], and I want to access the last element, that is arr[ 4 ];

Summary:
Arrays are accessed using subscripts, subscripts start from 0, and the subscript of the last element is the defined length - 1;

3. The storage method of one-dimensional array in memory

As the subscript of the array increases, the address of the element also increases regularly. The array is stored continuously in the element

#define _CRT_SECURE_NO_WARNINGS 1

#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

2. Two-dimensional array

1, what is a two-dimensional array

A two-dimensional array can be understood as multiple one-dimensional arrays that form rows and columns.

#include<stdio.h>
int main()
{
    
    
	int arr1[5] = {
    
    0,1,2,3,4,};
	int arr2[5] = {
    
    5,6,7,8,9};
	int arr3[5] = {
    
     10,11,12,13,14 };
	

	return 0;
}

Use a two-dimensional array to represent the position of the element
insert image description here

2. Creation of two-dimensional array

Similar to a one-dimensional array, but with a [ ] behind it

Array name [ ][ ]
The first [ ] represents the row of the array;
the second [ ] represents the column of the array;

int arr[3][4];
char arr[3][5];
double arr[2][4];

3. Initialization of two-dimensional array

The two-dimensional array initialization row can be omitted, but the column cannot be omitted . If you imagine giving you a row of numbers, you just tell you the number of rows and you don’t know how to arrange them, but you can do it until the number of columns. If you don’t believe me, try it.

int main()
{
    
    
	//数组的初始化
	int arr[4][5] = {
    
     {
    
    1,2,3,4,5},{
    
    2,3,4,5,6},{
    
    3,4,5,6,7},{
    
    5,6,7,8,9} };
	int arr2[4][5] = {
    
     {
    
    1,2,3 }, {
    
    2, 3, 4 }, {
    
    3, 4, 5, 6, 7}, {
    
    5, 6, 7, 8, 9} };
    //二维数组即使初始化的了
    //行是可以省略的,但是列是不能省略的
	int arr3[][5] = {
    
     {
    
    1,2,3 }, {
    
    2, 3, 4 }, {
    
    3, 4, 5, 6, 7}, {
    
    5, 6, 7, 8, 9} };

	return 0;
}

4. The use of two-dimensional arrays

The use of two-dimensional arrays is also through subscripts

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

insert image description here

5. Storage of two-dimensional arrays in memory

Like a one-dimensional array, as the array subscript increases, the address of the element also increases regularly. The elements of a two-dimensional array are stored continuously in the memory.

int main()
{
    
    
	int arr[4][5] = {
    
     0 };
	int i = 0;
	//行号
	for (i = 0; i < 4; i++)
//	{
    
    
		int j = 0;
	for (j = 0; j < 5; j++)
	{
    
    
		printf("&arr[%d][%d] = %p\n",i,j, &arr[i][j]);
		}
	}
	return 0;
}

insert image description here

Third, the array is out of bounds

Array subscripts are limited in scope.
> The subscript of the array is specified to start 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, 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 check the out-of-bounds of the array subscript, and the compiler may not report an error, but if the compiler does not report an error, it does not mean that the program is correct, so when writing code, pay attention to checking the out-of-bounds of the array

int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int i = 0;
	//0~10
	
	//
	for (i = 0; i <= 10; i++)//当i = 10时,就越界访问了
	{
    
    
		printf("%d ", arr[i]);
	}

	return 0;
}

insert image description here

4. Arrays as function parameters

We mentioned earlier that the array name represents the address of the first element of the array, so when the array name is used as a function parameter, only the address of the first element is actually passed, not the entire array, so you want to find the length of the array in the function. It is not possible (you only have the address of the first element, so you just get the byte length of the first element!!!) So we need to pass the length of the array as a function parameter when the main function asks for the length of the array. This is especially important! ! !

For example: enter 10 numbers, and then use the bubbling method to perform ascending order

int main()
{
    
    
	void bubble_sort(int arr[10], int sz);
	int arr[10] = {
    
     0 };
	//输入
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
    
    
		scanf("%d", &arr[i]);
	}
	//排序 - 升序
	bubble_sort(arr,sz);//让这个函数来完成数组arr中数据的排序
	for (i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}

	return 0;
}


void bubble_sort(int*arr, int sz)//这里的arr的本质是指针
{
    
    
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
    
    
		int j = 0;
		//一趟每部比较的对数
		for (j = 0; j < sz - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				//交换
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
	}
}

insert image description here

It’s not easy to create, give me a free like~
Please add a picture description

Guess you like

Origin blog.csdn.net/LHY537200/article/details/130533726