[Elementary C language]: The trap of arrays in interviews

★Array

Array: is a set of collections of the same type, one-dimensional array, two-dimensional array (matrix), three-dimensional array and character array.

●One-dimensional array


The creation of a one-dimensional array: directly on the example (int arr1[10]). Note: When the array is created, the constants in [ ] can be guessed, but variables cannot be used.
◆Trap: The variable modified by the const keyword is a variable with constant attributes in the C language, and it is still a variable, which cannot be placed in the [ ] of the array; however, the variable modified by const in C++ is a constant and can be placed in the [ ] of the array. in [ ] of the array.

Initialization of one-dimensional arrays: Give some reasonable initial values ​​to the contents of the array while creating the array. (Example below:)
int arr1[10]={1,2,3}
//The initial value of the array is 1, 2, 3, 0, 0, 0, 0, 0, 0, 0
int arr2[]={1,2,3,4}
//The initial value of the array is 1, 2, 3, 4
int arr3[5]={1,2,3,4,5}
char arr4[]={'a','b','c'}
char arr5[]="abcdef"

Trap:

char arr1[]="abc"//It can be printed in the form of %s, and the value calculated by sizeof is 4, because there is a '\0' character end mark
char arr2[3]={'a','b','c'};//Cannot be printed in the form of %s, because there is no '\0' character end mark, the printed result is a random value
char *p="abcdef"//p is a pointer variable, which stores the address of the first element a

Use of one-dimensional arrays:

1. The use of arrays is accessed through subscripts, which start from 0.
2. The size of the array can be obtained by sizeof(arr)/sizeof(arr[0]).

Storage of one-dimensional arrays in memory: Arrays are stored contiguously in memory (see what is the output of the following example?)
#include<stdio.h>

intmain()
{
	int arr[5] = { 0 };
	int  i = 0;
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]);i++)\
	{
		printf("&arr[%d]=%p\n", i, &arr[i]);
	}
	system("pause");
	return 0;
}
Output result:
Pointer access to 1D arrays: see the example below
#include<stdio.h>
#include<windows.h>

intmain()
{
	int arr[10] = { 0 };
	int i = 0;
	int *p = arr;//Assign the first element of the array to the pointer variable p
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		*(p + i) = i;//(p+i) is the address of the i-th element
	}
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%d\n", *(p + i));
	}
	system("pause");
	return 0;
}

◆The array name of a one-dimensional array is actually the address of the first element of the array. Through the operation of the array name + integer, the address of each element of the array can be obtained.
Trap:
printf("%p\n",arr); outputs the address of the first element of the array
printf("%p\n",&arr[0]); outputs the address of the first element of the array
printf("%p\n",&arr); The output is the address of the array, add 1 to it and skip the entire array

● two-dimensional array

The creation of a two-dimensional array: (directly on the example)
int arr[3][4];
char  arr[3][5];
double arr[2][4];

Initialization of two-dimensional array: (directly on the example)
int arr[3][4]={1,2,3,4};
int arr[3][4]={{1,2},{4,3}};
int arr[][4]={{2,3},{4,5}};

Trap: When the two-dimensional array is initialized, because it is also stored continuously in the memory, it can be regarded as a row, and the elements in the array can be determined according to the column. Therefore, the row can be omitted during initialization, but the column must not be omitted.


Use of two-dimensional arrays:
#include<stdio.h>
#include<windows.h>

intmain()
{
	int arr[3][4] = { 0 };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 4; j++)
		{
			arr[i][j] = i * 4 + j + 1;//Assign 1-12 to the two-dimensional array
		}
	}
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 4; j++)
		{
			printf("%d ", arr[i][j]);
		}
	}
	system("pause");
	return 0;
}

The use of two-dimensional array is also used according to the way of subscripting.

Storage of two-dimensional arrays in memory: continuous storage in memory (probably what the picture below looks like!)

Pointer access to 1D arrays: see the example below
#include<stdio.h>

intmain()
{
	int arr[3][4] = { 0 };
	int *p = &arr[0][0];//The address of the first element of the array
	int i = 0;
	for (i = 0; i < 12; i++)
	{
		*(p + i) = i;//(p+i) is the address of the i element of the array, and then dereferences it and assigns a value to each element of the array
	}
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 4; j++)
		{
			printf("%d ", arr[i][j]);
		}
	}
	return 0;
}
Trap: There are only two cases where the array name represents the entire array:

① sizeof (array name), used to find the size of the array.
②& array name, the address of the entire array is taken out

High energy ahead! ! ! The next article is about operations on arrays, which is very important.

▲Operations related to arrays
One-dimensional array:
#include<stdio.h>

intmain()
{
	int a[] = { 1, 2, 3, 4 };
	printf("%d\n", sizeof(a));// 16 where a represents the entire array
	printf("%d\n", sizeof(a+0));//4 address of the first element
	printf("%d\n", sizeof(*a));//4 size of a[0] type
	printf("%d\n", sizeof(a+1));//4 address of the second element
	printf("%d\n", sizeof(a[1]));//4 size of a[1] type
	printf("%d\n", sizeof(&a));//4 The address of the entire array, which is the same as the address of the first element, but the addition of 1 is different
	printf("%d\n", sizeof(*&a));//16 Dereference the address of the array
	printf("%d\n", sizeof(&a+1));//4 points to the pointer (address) behind 4
	printf("%d\n", sizeof(&a[0]));//4 address of a[0]
	printf("%d\n", sizeof(&a[0]+1));//4 address of a[1]

	return 0;
}

Character array:
#include<stdio.h>
#include<string.h>

intmain()
{
	char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
	//sizeof
	printf("%d\n", sizeof(arr));//6 The array has 6 characters, there is no '/0', they are all character types
	printf("%d\n", sizeof(arr+0));//4 the address of the first element of the array
	printf("%d\n", sizeof(*arr));//The space occupied by the type of 1 a
	printf("%d\n", sizeof(arr[1]));//The space occupied by the type of 1 b
	printf("%d\n", sizeof(&arr));//4 the address of the entire array
	printf("%d\n", sizeof(&arr+1));//4 pointer to the back of f
	printf("%d\n", sizeof(&arr[0]+1));//4 the address of the second element
	//strlen
	printf("%d\n", strlen(arr));//The random value array does not have the '\0' end sign, so it cannot stop
	printf("%d\n", strlen(arr+0));//The address of the first element of the random value array, there is no '\0'
	printf("%d\n", strlen(*arr));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(arr[1]));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(&arr));//Address of random value array, no '\0'
	printf("%d\n", strlen(&arr+1));//The random value points to the pointer behind f
	printf("%d\n", strlen(&arr[0]+1));//The address of the second element of the random value
	return 0;
}
#include<stdio.h>
#include<string.h>

intmain()
{
	char arr[] = "abcdef";
	//sizeof
	printf("%d\n", sizeof(arr));//7 //The array has a total of 7 characters, including '/0', their types are all characters
	printf("%d\n", sizeof(arr+0));//4 The address of the first element, the pointer variable is 4 bytes
	printf("%d\n", sizeof(*arr));//1 arr[0], the type of a is character
	printf("%d\n", sizeof(arr[1]));//1 b's type is character
	printf("%d\n", sizeof(&arr));//4 the address of the entire array
	printf("%d\n", sizeof(&arr+1));//4 pointer to the arr array
	printf("%d\n", sizeof(&arr[0]+1));//4 the address of the second element
	//strlen
	printf("%d\n", strlen(arr));//6 does not count '\0', there are 6 characters
	printf("%d\n", strlen(arr+0));//6 the address of the first element of the array
	printf("%d\n", strlen(*arr));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(arr[1]));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(&arr));//6 The address of the array, this is the same as the address of the first element but has a different meaning
	printf("%d\n", strlen(&arr+1));//The random value points to the pointer behind f
	printf("%d\n", strlen(&arr[0]+1));//5 the address of the second element
	
}
#include<stdio.h>
#include<string.h>

intmain()
{
	char *p = "abcdef";
	//sizeof
	printf("%d\n", sizeof(p));//4 p is a pointer variable, which is the address of a
	printf("%d\n", sizeof(p+1));//4 the address of the second element
	printf("%d\n", sizeof(*p));//1 *p=a, the type of a is char
	printf("%d\n", sizeof(p[0]));//1 is equivalent to dereferencing p
	printf("%d\n", sizeof(&p));//4 address of the pointer variable p, which is a secondary pointer
	printf("%d\n", sizeof(&p+1));//4 is still the address
	printf("%d\n", sizeof(&p[0]+1));//Address of the second element
	//strlen
	printf("%d\n", strlen(p));//6 Count from the first element to the end of '\0'
	printf("%d\n", strlen(p + 1));//5 the address of the second element, counting from the second
	printf("%d\n", strlen(*p));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(p[0]));//Wrong expression strlen function accepts a pointer, not a value
	printf("%d\n", strlen(&p));//Random value, the secondary pointer cannot be determined
	printf("%d\n", strlen(&p + 1));//Random value
	printf("%d\n", strlen(&p[0] + 1));//5 the address of the second element
}

Trap: For the above three operations on character arrays, you must master the difference between the sizeof operator and the strlen function. If you don't know, please poke --> Difference .
Two-dimensional array
#include<stdio.h>
#include<string.h>

intmain()
{
	int a[3][4] = { 0 };
	printf("%d\n", sizeof(a));//48 The array name here represents the entire array
	printf("%d\n", sizeof(a[0][0]));//4 The type of the first element is int
	printf("%d\n", sizeof(a[0]));//16 You can use a[0] as the array name of the first line array
	printf("%d\n", sizeof(a[0]+1));//4 a[0] is downgraded to the address of a[0][0], adding 1 becomes a[0][1] the address of
	printf("%d\n", sizeof(a+1));//4 a is downgraded to the address of the first line of the array, and 1 is added to the address of the second line
	printf("%d\n", sizeof(&a[0]+1));//4 Address of the second line
	printf("%d\n", sizeof(*a));//16 a is downgraded to the address of the first line of the array, and is dereferenced, which is equivalent to sizeof(a[0])
	printf("%d\n", sizeof(a[3]));//16 The expression inside sizeof does not operate, the type of a[3] is deduced from a[0], which is equivalent to a[0] of
	return 0;
}

Arrays as function parameters: (see the example below, is this okay?)
void binary_search(int arr[]);
{  
       int sz=sizeof(arr)/sizeof(arr[0]);
        ....
        ....
}
Trap: When an array is used as a function parameter, the entire array is not passed, but the address of the first element of the array is passed, so the calculated value of sizeof(arr)/sizeof(arr[0]) is 1. If the length of the array is to be used, it should be passed directly to the address as a parameter. Here int arr[ ] represents a pointer, which is equivalent to int *arr.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325950417&siteId=291194637