[C Language] Array and pointer practice

We have almost learned about pointers and arrays. Today I will share with you some common practice questions about pointers and arrays, including many classic interview questions!


1. Find the length and size of the array

Ordinary one-dimensional array

int main()
{
    
    
	//一维数组
	int a[] = {
    
     1,2,3,4 };
	printf("%d\n", sizeof(a));//整个数组大小16
	printf("%d\n", sizeof(a + 0));//首元素地址8
	printf("%d\n", sizeof(*a));//首元素4
	printf("%d\n", sizeof(a + 1));//第二个元素地址8
	printf("%d\n", sizeof(a[1]));//第二个元素4
	printf("%d\n", sizeof(&a));//整个数组地址 8
	printf("%d\n", sizeof(*&a));//先拿到这个数组地址,再解引用拿到整个数组(即:数组名)-》16
	printf("%d\n", sizeof(&a + 1));//地址8
	printf("%d\n", sizeof(&a[0]));//第一个元素地址8
	printf("%d\n", sizeof(&a[0] + 1));//第二个元素地址8

	return 0;
}

character array

int main()
{
    
    
	//字符数组
	char arr[] = {
    
     'a','b','c','d','e','f' };
	printf("%d\n", sizeof(arr));//整个数组 6
	printf("%d\n", sizeof(arr + 0));//数组首元素地址8
	printf("%d\n", sizeof(*arr));//第一个元素1
	printf("%d\n", sizeof(arr[1]));//第一个元素1
	printf("%d\n", sizeof(&arr));//整个数组地址8
	printf("%d\n", sizeof(&arr + 1));//地址8
	printf("%d\n", sizeof(&arr[0] + 1));//第二个元素地址8
	printf("%d\n", strlen(arr));//随机值
	printf("%d\n", strlen(arr + 0));//随机值
   /* printf("%d\n", strlen(*arr));
	printf("%d\n", strlen(arr[1]));*/
	printf("%d\n", strlen((const char*) & arr));
	printf("%d\n", strlen((const char*)(&arr + 1)));
	printf("%d\n", strlen(&arr[0] + 1));//随机值

	return 0;
}

string array

int main()
{
    
    
	char arr[] = "abcdef";
	printf("%d\n", sizeof(arr));//这个数组7
	printf("%d\n", sizeof(arr + 0));//第一个元素地址8
	printf("%d\n", sizeof(*arr));//第一个元素1
	printf("%d\n", sizeof(arr[1]));//第一个元素1
	printf("%d\n", sizeof(&arr));//这个数组地址8
	printf("%d\n", sizeof(&arr + 1));//地址8
	printf("%d\n", sizeof(&arr[0] + 1));//第二个元素地址8
	printf("%d\n", strlen(arr));//数组长度6
	printf("%d\n", strlen(arr + 0));//6
	/*printf("%d\n", strlen(*arr));
	printf("%d\n", strlen(arr[1]));
	printf("%d\n", strlen(&arr));
	printf("%d\n", strlen(&arr + 1));*/
	printf("%d\n", strlen(&arr[0] + 1));//5

	return 0;
}

character pointer to string

int main()
{
    
    
	const char* p = "abcdef";
	printf("%d\n", sizeof(p));//指针,大小8
	printf("%d\n", sizeof(p + 1));//指针,大小8
	printf("%d\n", sizeof(*p));//第一个字符a,大小1
	printf("%d\n", sizeof(p[0]));//第一个字符a,大小1
	printf("%d\n", sizeof(&p));//指针的地址,大小8
	printf("%d\n", sizeof(&p + 1));//指针的地址,大小8
	printf("%d\n", sizeof(&p[0] + 1));//第二个元素地址,大小8
	printf("%d\n", strlen(p));//长度,6
	printf("%d\n", strlen(p + 1));//长度,5
	/*printf("%d\n", strlen(*p));
	printf("%d\n", strlen(p[0]));
	printf("%d\n", strlen(&p));
	printf("%d\n", strlen(&p + 1));*/
	printf("%d\n", strlen(&p[0] + 1));//长度,5

	return 0;
}

Two-dimensional array

//int main()
//{
    
    
//	int a[3][4] = { 0 };
//	printf("%d\n", sizeof(a));//整个数组大小,48
//	printf("%d\n", sizeof(a[0][0]));第一个元素,大小4

//	printf("%d\n", sizeof(a[0]));//第一行数组的数组名,大小16
// a[0]是第一行一维数组的数组名,数组名单独放在了sizeof里面,就表示整个数组,所以就算的就算整个数组的大小 为16


//	printf("%d\n", sizeof(a[0] + 1));//第一行第二个元素地址,8
//	printf("%d\n", sizeof(*(a[0] + 1)));//第一行第二个元素,大小4
//	printf("%d\n", sizeof(a + 1));//第二行的地址,大小8
//	printf("%d\n", sizeof(*(a + 1)));//第二行数组的数组名,大小16
//	printf("%d\n", sizeof(&a[0] + 1));//第二行数组地址,大小8
//	printf("%d\n", sizeof(*(&a[0] + 1)));//第二行数组数组名,大小16
//	printf("%d\n", sizeof(*a));//第一行数组数组名,大小16
//	printf("%d\n", sizeof(a[3]));//数组名,大小16
//
//	return 0;
//}

/*
二维数组有的情况下可以拿到某一行数组的数组名,这时放在sizeof中就算的也是那行数组的大小
二维数组就是数组的数组,,就是一维数组的数组
eg:
a[3][4]    a[0]就是第一行数组的数组名,sizeof算他的大小为第一行整个数组的大小   为:16
*/

General section:

The meaning of array names:

  1. sizeof(array name), the array name here represents the entire array, and the size of the entire array is calculated.
  2. &Array name, the array name here represents the entire array, and the address of the entire array is taken out.
  3. In addition, all array names represent the address of the first element.

2. Question types related to pointers

Written test question 1:

int main()
{
    
    
   
        int a[5] = {
    
     1, 2, 3, 4, 5 };
        int* ptr = (int*)(&a + 1);
        printf("%d,%d", *(a + 1), *(ptr - 1));//2   5
        return 0;
    


    //程序的结果是什么?


	return 0;
}

Insert image description here

Written test question 2:

struct Test
{
    
    
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
//已知,结构体Test类型的变量大小是20个字节
int main()
{
    
    
	p = (struct Test*)0x100000;
	printf("%p\n", p + 0x1);//0x100014
	printf("%p\n", (unsigned long)p + 0x1);//0x100001
	printf("%p\n", (unsigned int*)p + 0x1);//0x100004
	return 0;
}

analyze:

  1. p is the structure pointer. +1 is equivalent to skipping a structure size. Here we tell that the size of the structure is 20 bytes. When p + 0x1 is printed in hexadecimal, it is 0x100014.
  2. p is converted to an unsigned long integer, not a pointer, +1 is +1, 0x100001
  3. p is converted to an unsigned integer type pointer + 1 and skips four bytes. 0x100004
    is printed with %p, the first one is 00100014, the second one is 00100001, and the third one is 00100004

Written test question 3:

int main()
{
    
    
	int a[4] = {
    
     1, 2, 3, 4 };
	int* ptr1 = (int*)(&a + 1);
	int* ptr2 = (int*)((int)a + 1);
	printf("%x,%x", ptr1[-1], *ptr2);//4   2000000
	//%x打印16进制
	//%o打印8进制
	return 0;
}

Insert image description here

Written test question 4:

int main()
{
    
    
	int a[3][2] = {
    
     (0, 1), (2, 3), (4, 5) };
	int* p;
	p = a[0];//p就算第一个元素地址
	printf("%d", p[0]);//
	return 0;
}

Insert image description here

Written test question 5:

int main()
{
    
    
	int a[5][5];
	int(*p)[4];
	p = (int(*)[4]) a;
	printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
	return 0;
}

Insert image description here
So the subtraction is -4
Insert image description here

Written test question 6:

//int main()
//{
    
    
//	int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//	int* ptr1 = (int*)(&aa + 1);
//	int* ptr2 = (int*)(*(aa + 1));
//	printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));//10 5
//	return 0;
//}

Insert image description here

Written test question 7:

int main()
{
    
    
	const char* a[] = {
    
     "work","at","alibaba" };
	const char** pa = a;
	pa++;
	printf("%s\n", *pa);//       at
	return 0;
}

Here is a distribution diagram of the relationships:
Insert image description here

Written test question 8:

int main()
{
    
    
	const char* c[] = {
    
     "ENTER","NEW","POINT","FIRST" };
	const char** cp[] = {
    
     c + 3,c + 2,c + 1,c };
	const char*** cpp = cp;
	printf("%s\n", **++cpp);//POINT
	printf("%s\n", *-- * ++cpp + 3);//ER
	printf("%s\n", *cpp[-2] + 3);//ST
	printf("%s\n", cpp[-1][-1] + 1);//EW
	return 0;
}

Insert image description here
1. ++cpp, cpp points to c+2, dereferences to get c+2, dereferences to get the first address of POINT, and then prints out POINT according to %s 2. ++cpp, makes cpp point to c+1
, Dereference to get c+1, then – (c+1) to point to c, then dereference to get the address of ENTER, then +3 to get the address of E, and print out the ER class method based on %s, this is what it looks like
, All can be solved in turn

Summary:
Today’s pointer array practice questions are shared here. These question types can help us have a deeper grasp of arrays and pointers. Thank you everyone! ! !

Guess you like

Origin blog.csdn.net/m0_71214261/article/details/133171034