Station C is the easiest to get started with and teaches you how to learn arrays by hand

1. Creation and initialization of one-dimensional arrays

create: Take an integer array as an example

	int arr[5] = {
    
     1,2,3,4,5 };

The shape is like this, arrfor the created array name
[5], the array inside is the number of elements of the array,
intindicating that the type of the elements of the array is an integer type
initialization

	int arr[5] = {
    
     1,2,3,4,5 };
	这种是把数组里面的元素全部初始化,并且指定了数组元素的个数
	int arr1[] = {
    
     1,2,3,4,5 };
	这种没有指定数组的个数,数组的元素的个数根据后面大括号里面的个数确定
	int arr2[5] = {
    
     1,2 };
	这种指定了数组元素的大小却没有完全初始,
	前面两个元素分别被初始化为12后面3个元素都被默认初始化为0

character array

char arr[]="maoleshua";
这是10个元素,因为字符串以\0为结尾
char arr[]={
    
    'm','l','n','b'};
这是4个元素

think

int n=5;
int arr[n]={
    
    1,2,3,4,5};

Is the above code correct? Before the C99 standard, it was wrong to write this way ( [] can only contain integer constants ), but after that, there is the concept of variable-length arrays, which is correct.
But the vs compiler does not support the C99 standard for the time being. So it is wrong.

2. The use of one-dimensional arrays

easy to use

int main()
{
    
    
	int arr[5] = {
    
     1,2 };
	int i = 0;
	for (i = 0; i < 5; i++)
	{
    
    
		arr[i] = i;
	}
	for (i = 0; i < 5; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

3. Storage of one-dimensional arrays in memory

The array is stored contiguously in memory.
For the above code,
look at its memory address
As can be seen

4. Creation and initialization of two-dimensional arrays

The two-dimensional array looks like this int arr[][], the first [ ] represents the row, and the second [ ] represents the column.
initialization:

	int arr1[2][2] = {
    
     1,2,3 ,4};
	全部初始化
	int arr2[2][2] = {
    
     {
    
    1,2},{
    
    3 ,4} };
	和上面的一样,每个大括号表示一行
	int arr3[2][2] = {
    
     {
    
    1},{
    
    2,3 } };
	这种第一行没有完全初始化,没有完全初始化的被默认初始化为0
	int arr4[][2] = {
    
     1,2,3 ,4};
	这种省略行的表示是正确的,行数由后面的初始化的确定
	int arr5[2][] = {
    
     1,2,3,4 };
	这种初始化是错误的,不能省略列
	int arr6[2][2] = {
    
     1 };
	非完全初始化,其余元素都为0

5. The use of two-dimensional arrays

It's that simple to use

	int arr[2][2] = {
    
     1,2,3 ,4};
	int i, j;
	for(i=0;i<2;i++)
	{
    
    
		for (j=0;j<2;j++)
		{
    
    
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

6. Storage of two-dimensional arrays in memory

Two-dimensional arrays are also stored contiguously in memory

7. Array out of bounds

Array out of bounds means accessing memory outside the array.
Take a one-dimensional array as an example

	int arr[5] = {
    
     1,2 };
	int i = 0;
	for (i = 0; i < 6; i++)这里当i==5,访问数组中第6个元素,为越界访问
	{
    
    
		arr[i] = i;
	}

8. Arrays as function parameters

The array name of a one-dimensional array represents the address of the first element . The array name of a two-dimensional array represents the address of the element of the
first row . Calculates the size of the entire array, in bytes


void f(int* arr)
{
    
    
	int st = sizeof(arr) / sizeof(arr[0]);
	大家可能以为st的值为5,但是为1,
	因为这个arr是指针变量,32位平台上占4个字节
}
int main()
{
    
    

	int arr[5] = {
    
     1,2,3 ,4 };
	f(arr);
	
	return 0;
}

Readers must pay attention

9. Teach you the group by hand

QQ group 720429262

Guess you like

Origin blog.csdn.net/m0_60598323/article/details/123087226