当前位置:网站首页>Analysis of pointer and array written test questions

Analysis of pointer and array written test questions

2022-07-06 08:03:00 Pick up the famous Rose


Preface

I haven't seen you for a long time !!! Recently, due to the school exam , I haven't updated my blog , It's not until these two days that I finish the school exam , Ha ha ha ha ha ha , I can't wait to explain it to you !!!
Today I'll tell you about the interview questions about arrays and pointers , The content may be difficult , There are knowledge points that you can refer to

Advanced pointer

Another thing about this host is 32 Bit environment , So the default size of the pointer is 4 Bytes !!!


One 、 Pointer and array written test question analysis

1. One dimensional array

int main() {
    

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

	//sizeof( Array name ), The array name represents the entire array , It calculates the size of the entire array , Unit is byte 
	printf("%d\n", sizeof(a));//16
	//a Not alone sizeof Inside , No address , therefore a Is the first element address ,a+0 Or the address of the first element 
	printf("%d\n", sizeof(a + 0));//4/8
	//*a Medium a Is the address of the first element of the array ,*a Is to dereference the address of the first element , What you find is the first element 
	// The size of the first element is 4 Bytes 
	printf("%d\n", sizeof(*a));//4
	// here a Address of the first element of the array 
	//a+1 Is the address of the second element 
	//sizeof(a+1) Is the size of the address 
	printf("%d\n", sizeof(a + 1));//4/8
	// It calculates the size of the second element 
	printf("%d\n", sizeof(a[1]));//4
	//&a The address of the extracted array , Address of array , It's just an address 
	printf("%d\n", sizeof(&a));//4/8
	//&a------>int(*)[4]
	// &a What you get is the address of the array name , The type is int(*)[4], Is a set of array pointers 
	// Array pointer dereference is equivalent to accessing an array 
	// &*a---->a
	//2.
	//* and & Offset   amount to sizeof(a)
	printf("%d\n", sizeof(*&a));//16
	//&a--> What is taken out is the pointer of the array 
	//&a-->int(*)[4]
	//&a+1  Is from an array a The address of skipped a backward (4 Of shaping elements ) Size of array 
	//&a+1 Or the address , Yes, the address is 4 Bytes 
	printf("%d\n", sizeof(&a + 1));//4/8
	//&a[0] It's the address of the first element 
	// It calculates the size of the address 
	printf("%d\n", sizeof(&a[0]));//4/8
	//&a[0]+1 Is the address of the second element 
	// It calculates the size of the address 
	//&a[0]+1---->&a[1]
	printf("%d\n", sizeof(&a[0] + 1));//4/8

	return 0;
}

 Insert picture description here
 Insert picture description here

2. A character array

int main() {
    
	char arr[] = {
     'a','b','c','d','e','f' };
	//sizeof( Array name )
	printf("%d\n", sizeof(arr));//6
	//arr+0 Is the address of the first element of the array 
	printf("%d\n", sizeof(arr + 0));//4/8
	//*arr Is the first element of the array , The size is one byte 
	//*arr--->arr[0]
	//*(arr+0)---->arr[0]
	printf("%d\n", sizeof(*arr));//1
	//*arr Is the second element of the array , The size is one byte 
	printf("%d\n", sizeof(arr[1]));//1
	//&arr Is the address of the array , Yes, the address is 4/8 Bytes 
	printf("%d\n", sizeof(&arr));//4/8
	//&arr + 1 Is the address after the array 
	printf("%d\n", sizeof(&arr + 1));//4/8
	//(&arr[0] + 1) Is the second element of the array , The size is one byte 
	printf("%d\n", sizeof(&arr[0] + 1));//4/8


	printf("%d\n", strlen(arr));// Random value 
	printf("%d\n", strlen(arr + 0));// Random value 
	//printf("%d\n", strlen(*arr));//--->strlen('a');--->strlen(97);// Wild pointer 
	 There is a problem 
	--->strlen('b');--->strlen(98);// Wild pointer 
	//printf("%d\n", strlen(arr[1]));


	printf("%d\n", strlen(&arr));// Random value  
	printf("%d\n", strlen(&arr + 1));// Random value -6
	printf("%d\n", strlen(&arr[0] + 1));// Random value -1

	return 0;
}

 Insert picture description here


int main() {
    
	char arr[] = "abcdef";

	printf("%d\n", sizeof(arr));//7
	printf("%d\n", sizeof(arr + 0));//4/8
	printf("%d\n", sizeof(*arr));//1
	printf("%d\n", sizeof(arr[1]));//1
	printf("%d\n", sizeof(&arr));//4/8
	printf("%d\n", sizeof(&arr + 1));//4/8
	printf("%d\n", sizeof(&arr[0] + 1));//4/8



	//strlen Is to find the length of the string , Focus on... In the string \0, The calculation is \0 The number of characters that appear before 
	//strlen It's a library function , For strings only 
	//sizeof Only pay attention to the size of memory space , Don't care what's in the memory 
	//sizeof It's the operator 
	printf("%d\n", strlen(arr));//6
	printf("%d\n", strlen(arr + 0));//6

	 There is a problem 
	//printf("%d\n", strlen(*arr));
	//printf("%d\n", strlen(arr[1]));

	//
	printf("%d\n", strlen(&arr));//6
	printf("%d\n", strlen(&arr + 1));// Random value 
	printf("%d\n", strlen(&arr[0] + 1));//5

	return 0;
 }

 Insert picture description here
 Insert picture description here

3. Two dimensional array

int main() {
    

	// Two dimensional array 
	int a[3][4] = {
     0 };
	printf("%d\n", sizeof(a));//48
	printf("%d\n", sizeof(a[0][0]));//4

	//a[0] Is the array name of this one-dimensional array in the first line , Put it alone sizeof Inside ,a[0] Represents the first entire one-dimensional array 
	//sizeof(a[0]) Calculate the size of this line 
	printf("%d\n", sizeof(a[0]));//16

	//(a[0] + 1)--->&a[0][0]+1
	//a[0] Not alone in sizeof Inside , I didn't take the address 
	//a[0] It means the address of the first element , Is the address of the first element of the first row of this one-dimensional array 
	//a[0]+1 Is the address of the second element in the first line 
	printf("%d\n", sizeof(a[0] + 1));//4/8

	printf("%d\n", sizeof(*(a[0] + 1)));//4
	//a Although it is the address of a two-dimensional array , But it is not placed alone sizeof Inside , I didn't take the address 
	//a Represents the address of the first element , The first element of a two-dimensional array is its first line ,a It's the address on the first line 
	//a+1 Just skip the first line , Indicates the address of the second line 
	printf("%d\n", sizeof(a + 1));//4/8
	//*(a + 1) Is the dereference of the address in the second line , I got the second line 
	//*(a+1)--->a[1]
	//sizeof(*(a+1))--->sizeof(a[1])
	printf("%d\n", sizeof(*(a + 1)));//16
	//&a[0]-- Address the array name in the first row , Which is the address in the first line 
	//&a[0]+1-- What you get is the address on the second line 
	printf("%d\n", sizeof(&a[0] + 1));//4/8

	printf("%d\n", sizeof(*(&a[0] + 1)));//16
	//a Represents the address of the first element , It's the address on the first line 
	//*a Is the dereference of the address in the first line , What you get is Yixing 
	printf("%d\n", sizeof(*a));//16

	printf("%d\n", sizeof(a[3]));//16


	return 0;
}

 Insert picture description here
 Insert picture description here

summary :
The meaning of array names :

  1. sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
  2. & Array name , The array name here represents the entire array , It takes out the address of the entire array .
  3. In addition, all array names represent the address of the first element .

Two 、 Pointer written test questions

1. Interview questions 1

int main()
{
    
	int a[5] = {
     1, 2, 3, 4, 5 };
	int* ptr = (int*)(&a + 1);//&a The type is  int(*)[5], So you need to force the type to be converted to int*
	printf("%d,%d", *(a + 1), *(ptr - 1));
	return 0;
}
// What is the result of the program ?

 Insert picture description here
 Insert picture description here

2. Interview questions 2

// Because I haven't learned the structure yet , The size of the structure is 20 Bytes 
struct Test
{
    
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p= (struct Test*)0x100000;
// hypothesis p  The value of is 0x100000.  What are the values of the expressions in the following table ?
// It is known that , Structure Test The variable size of type is 20 Bytes 
int main()
{
    
	printf("%p\n", p + 0x1);//0x00100014
	//0x100000+20--->//0x100014
	printf("%p\n", (unsigned long)p + 0x1);//0x00100001
	//1,048,576+1--->1,048,577
	//0x100001
	printf("%p\n", (unsigned int*)p + 0x1);//0x00100004
	//0x100000+4--->//0x100004
	return 0;
}

 Insert picture description here
 Insert picture description here

3. Interview questions 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);
	return 0;
}

 Insert picture description here

 Insert picture description here

4. Interview questions 4

int main()
{
    
	int a[3][2] = {
     (0, 1), (2, 3), (4, 5) };
	int* p;
	p = a[0];//a[0] Is the array name in the first row 
	//a[0] Represents the address of the first element , namely a[0][0] The address of ,&a[0][0]
	printf("%d", p[0]);
	return 0;
}

 Insert picture description here
 Insert picture description here

5. Interview questions 5

int main()
{
    
	int a[5][5];
	int(*p)[4];
	p = a;
	printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);//-4 -4
	// The value of address subtraction is the number of intermediate elements 
	return 0;
}

 Insert picture description here
 Insert picture description here

6. Interview questions 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));
	return 0;
}

 Insert picture description here
 Insert picture description here

7. Interview questions 7

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

 Insert picture description here
 Insert picture description here

8. Interview questions 8

int main()
{
    
	char* c[] = {
     "ENTER","NEW","POINT","FIRST" };
	char** cp[] = {
     c + 3,c + 2,c + 1,c };
	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 picture description here
 Insert picture description here
 Insert picture description here
What we should pay attention to here is ++cpp Yes, it will cpp The address of has changed
 Insert picture description here
 Insert picture description here
 Insert picture description here


summary

Tips : Here is a summary of the article :
for example : That's what we're going to talk about today , This article only briefly introduces the analysis of interview questions about arrays and pointers , This article is a little too much , We need to understand it slowly , You can talk about me privately if you don't understand !!!

原网站

版权声明
本文为[Pick up the famous Rose]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060758057655.html