C Language Notes: Arrays

content

1. Creation and initialization of one-dimensional arrays

1.1 Array creation

1.2 Initialization of the array

1.3 The use of one-dimensional arrays

1.4 Storage of one-dimensional arrays in memory

2. Creation and initialization of two-dimensional arrays

2.1 Creation of two-dimensional arrays

2.2 Initialization of two-dimensional arrays

2.3 The use of two-dimensional arrays

2.4 Storage of two-dimensional arrays in memory

3. Arrays as function parameters


1. Creation and initialization of one-dimensional arrays

1.1 Array creation

An array is a collection of elements of the same type .

How the array is created:

type_t   arr_name   [const_n];
//type_t 是指数组的元素类型
//const_n 是一个常量表达式,用来指定数组的大小

Note: For array creation, before the C99 standard, a constant must be given in [ ], and a variable cannot be used. The concept of variable-length arrays is supported in the C99 standard.

Writing like this will throw an error:

//错误定义方式
int count = 10;
int arr2[count]; 

1.2 Initialization of the array

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

Note 1: incomplete initialization: the remaining elements are initialized to 0 by default

int arr1[10] = {1,2,3}; 
int arr2[10] = { 0 }; 

Note 2: When initializing, you can not specify the size of the array, the size of the array will be determined according to the number of elements in { }

int arr3[] = {1,2,3}; //数组大小为3  

Note 3: Initialization of strings

#include <stdio.h>
#include <string.h>

char ch1[] = {'a', 'b', 'c', 'd'}; 
char ch2[] = "abcd";
printf("%d\n", sizeof(ch1));  //4
printf("%d\n", sizeof(ch2));  //5 '\0'也会被算进去
printf("%d\n", strlen(ch1));  //随机值,因为ch1中没有'\0'所以strlen会一直走,直到遇到'\0'为止
printf("%d\n", strlen(ch2));  //4

ch1 stores abcd and ch2 stores abcd'\0'

1.3 The use of one-dimensional arrays

#include <stdio.h>
int main()
{
    //数组的不完全初始化
    int arr[10] = { 0 };
    //计算数组的元素个数
    int sz = sizeof(arr) / sizeof(arr[0]);
    //对数组内容赋值,数组是使用下标来访问的,下标从0开始。所以:
    int i = 0;//做下标
    for (i = 0; i < sz; i++) //这里写10是不好的,当数组需要改动的时候这里需要跟着改,麻烦
    {
        arr[i] = i;
    }
    //输出数组的内容
    for (i = 0; i < sz; ++i)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

Summarize:

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

Observe the following code:

#include <stdio.h>
int main()
{
    //数组的不完全初始化
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    //计算数组的元素个数
    int sz = sizeof(arr) / sizeof(arr[0]);
    int i;
    //输出数组元素的地址
    for (i = 0; i < sz; ++i)
    {
        printf("%p\n", &arr[i]);
    }
    return 0;
}

The results are as follows:

It can be seen that as the index of the array grows, the address of the element also increases regularly - the address of adjacent elements differs by 4 bytes

Conclusion: One-dimensional arrays are stored contiguously in memory

Since it is stored contiguously in memory, all elements of the array can be accessed by the address of the first element :

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

Note: The array name arr represents the address of the first element of the array

2. Creation and initialization of two-dimensional arrays

2.1 Creation of two-dimensional arrays

//数组创建
int arr[3][4];    //三行四列
char arr[3][5];
double arr[2][4];

2.2 Initialization of two-dimensional arrays

//数组初始化
int arr[3][4] = {1,2,3,4};                 //除第一行外全是0
int arr[3][4] = {
   
   { 1, 2},{ 3, 4},{ 5 }};  //1,2存入第一行 3,4存入第二行, 5存入第三行
int arr[][4] = {
   
   { 2, 3},{ 4, 5}};         //二维数组如果有初始化,行可以省略,列不能省略

 Note: If the two-dimensional array is initialized, the row can be omitted, and the column cannot be omitted

2.3 The use of two-dimensional arrays

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

2.4 Storage of two-dimensional arrays in memory

Print the address of each element in a two-dimensional array:

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

The results are as follows:

Conclusion: Two-dimensional arrays are also stored contiguously in memory

So you can also access two-dimensional array elements like this:

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

3. Arrays as function parameters

Array as a parameter actually uploads the address of the first element of the array

#include <stdio.h>

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 - 1 - i; j++) //10个元素,第一堂比较9次,第二趟比较8次以此类推
		{
			if (arr[j] > arr[j + 1])
			{
				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 };
	//排序-排成升序
	int sz = sizeof(arr) / sizeof(arr[0]);//为了确定排序的趟数:10个元素需要冒泡排序9趟
	bubble_sort(arr,sz);//冒泡排序
	int* p = &arr;
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", *p);
		p++;
	}
	return 0;
}

In the above code, if you calculate sz inside bubble_sort, you can see that sz is 1 after debugging, because the first element 4/first element 4=1

So the number of arrays should be calculated outside the function and passed in directly

Similarly, when passing parameters in a two-dimensional array, you need to pass in the number of rows and columns

What is the array name?

Answer: The array name is the address of the first element of the array (with two exceptions)

Exception 1. sizeof(array name), which calculates the size of the entire array.

Exception 2. & array name, the address of the array is taken out.

Guess you like

Origin blog.csdn.net/m0_62934529/article/details/123379091