C language learning notes - P26 (< C language high level > + advanced level of pointer < 4 > + problem example + illustration)

How can efforts be failed? Water into the natural canal!

Disdain for luck, strength laughs to the end!

catalogue

9. Pointer and array written test question analysis

Summary:

Meaning of array name:

10. Pointer written test questions (selected written test questions of famous enterprises)

Written test question 1:

Written test question 2:

Item 3:

Item 4:

Written test question 5:

Pen test question 6:

Written test question 7:

Written test question 8:

Postscript: ● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice! -- By author: Xinxiao · old acquaintance

9. Pointer and array written test question analysis

Review of the previous chapter:

 

Analyze the following codes:

int main() 
{ //One dimensional array
	int a[] = { 1,2,3,4 };
	printf("%d\n", sizeof(a));        //The array name a is placed separately in sizeof. The size of the entire array is calculated in bytes, 4 * 4 = 16
	printf("%d\n", sizeof(a + 0));    //a represents the address of the first element of the array. a+0 is also the address of the first element of the array. The address size is 4 / 8
	printf("%d\n", sizeof(*a));       //A represents the address of the first element, * a is the dereference of the address of the first element, which is the first element, and the size is 4 bytes
	printf("%d\n", sizeof(a + 1));    //A represents the address of the first element, a+1 is the address of the second element, is the address, and the size is 4 / 8 bytes
	printf("%d\n", sizeof(a[1]));     //a[1] is the second element of the array, with a size of 4 bytes
	printf("%d\n", sizeof(&a));       //&A indicates the address of the array. The address of the array is also the address. The address size is 4 / 8 bytes
	printf("%d\n", sizeof(*&a));      //It can be understood as * and & offset effect, * & A is equivalent to a, and sizeof (a) is 16
								      //&a -> int(*)[4]
								      //&A is the address of the array. Its type is int(*)[4] array pointer. If you dereference, you will access an array of 4 ints with a size of 16 bytes
	printf("%d\n", sizeof(&a + 1));   //&A is the address of the array, and & A + 1 is the address after skipping the whole array. The address is 4 / 8
	printf("%d\n", sizeof(&a[0]));    //&A [0] get the address of the first element of the array. The address is 4 / 8
	printf("%d\n", sizeof(&a[0] + 1)); //&The size of the second element is 0 / 4, which is the address of the second element
	//&a[0] - int*
	return 0;
}

 

Analyze the following codes:

//Character array
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));         //As an array list, arr is placed in sizeof. The size of the whole array is calculated in bytes, 6
printf("%d\n", sizeof(arr + 0));     //arr is the address of the first element. arr+0 is also the address of the first element. The address size is 4 / 8
printf("%d\n", sizeof(*arr));        //Arr is the address of the first element, * arr is the first element, which is a character, the size is a byte, 1
printf("%d\n", sizeof(arr[1]));      //arr[1] is the second element of the array. It is a character with a size of 1 byte
printf("%d\n", sizeof(&arr));        //&Arr takes out the address of the array. The address of the array is also the address. The address is 4 / 8 bytes
printf("%d\n", sizeof(&arr + 1));    //&Arr takes out the address of the array, & arr + 1, skipping the whole array, & arr + 1 is still the address, and the address is 4 / 8 bytes
printf("%d\n", sizeof(&arr[0] + 1)); //&Arr [0] is the address of the first element, & arr [0] + 1 is the address of the second element, and the address is 4 / 8 bytes

#include<string.h>
int main()
{
	char arr[] = { 'a','b','c','d','e','f' };
	printf("%d\n", strlen(arr));     //arr is the address of the first element, but there is no \ 0 in the arr array. When calculating, you don't know when to stop. The result is: random value
    printf("%d\n", strlen(arr + 0)); //Arr is the address of the first element, and arr+0 is the address of the first element. The result is: random value
	printf("%d\n", strlen(*arr));    //err,strlen needs an address. Start from this address and look for characters backward until \ 0. Count the number of characters.
		//However, * arr is the first element of the array, that is, 'a'. This is the ascii code value 97 of 'a' passed to strlen. The strlen function will take 97 as the starting address and count the string, which will form a memory access conflict
	printf("%d\n", strlen(arr[1]));  //err as in the previous one, memory access conflicts
	printf("%d\n", strlen(&arr));    //&arr is the address of the arr array. Although the type is different from the parameter type of strlen, after passing the parameter, it is still from the first character
							         //The position is backward, and the result is still a random value.
	printf("%d\n", strlen(&arr + 1));    //Random value
	printf("%d\n", strlen(&arr[0] + 1)); //Random value
}

Analyze the following codes:

int main()
{
	char arr[] = "abcdef";	
	printf("%d\n", sizeof(arr));		  //arr is placed separately in sizeof, which calculates the size of the whole array. Because it is a string, it automatically adds' \ 0 ', a total of 7 bytes 
	printf("%d\n", sizeof(arr + 0));	  //arr is the address of the first element. arr+0 is also the address of the first element. The address is 4 / 8 bytes
	printf("%d\n", sizeof(*arr));	      //Arr is the address of the first element, * arr dereference is the first element a, char type, occupying 1 byte
	printf("%d\n", sizeof(arr[1]));		  //arr[1] is the size of the second element, char type, occupying 1 byte
	printf("%d\n", sizeof(&arr));		  //&Arr takes out the address of the whole array, which is 4 / 8 bytes
	printf("%d\n", sizeof(&arr + 1));	  //&Arr + 1 skipped. It is the address of the entire array, the address of '\ 0', and the address is 4 / 8 bytes
	printf("%d\n", sizeof(&arr[0] + 1));  //&Arr [0] + 1 is the address of the second element b, which is 4 / 8 bytes
}	

 

#include<string.h>
int main()
{
	char arr[] = "abcdef";
	printf("%d\n", strlen(arr));		  //In strlen, arr refers to the address of the first element a, which is 6 bytes in total until '\ 0' terminates
	printf("%d\n", strlen(arr + 0));	  //arr is the address of the first element. arr+0 is also the address of the first element. The effect is equivalent to strlen(arr), a total of 6 bytes
	printf("%d\n", strlen(*arr));	      //*arr is the first element a, the ASCII code value is 97, and strlen asks for the address, takes 97 as the address, illegally accesses memory and reports an error!
	printf("%d\n", strlen(arr[1]));	      //arr[1] is the second element b, and the ASCII code value is 98. strlen takes 98 as the address, illegally accesses memory and reports an error!
	printf("%d\n", strlen(&arr));		  //&Arr fetches the address of the whole array. The address of the whole array is still the starting address, a total of 6 bytes
	printf("%d\n", strlen(&arr + 1));	  //&Arr + 1 skips the address of the whole array. If the following is unknown, it is a random value
	printf("%d\n", strlen(&arr[0] + 1));  //This is the address of b, which is 5 bytes in total until '\ 0' terminates
}

 

 

 

 

 

 

 

Analyze the following codes:

int main()
{
	char* p = "abcdef";
	printf("%d\n", sizeof(p));         //p is a pointer variable. sizeof(p) calculates the size of the pointer variable, 4 / 8 bytes
	printf("%d\n", sizeof(p + 1));     //p is a pointer variable, which stores the address. p+1 is also the address. The address size is 4 / 8 bytes
	printf("%d\n", sizeof(*p));        //p is the pointer of char *, * p accesses 1 byte, and sizeof(*p) size is 1 byte
	printf("%d\n", sizeof(p[0]));      //P [0] -- > * (P + 0) - > * P, 1 byte
	printf("%d\n", sizeof(&p));        //&P is also an address, which is 4 / 8 bytes, &p is a secondary pointer
	printf("%d\n", sizeof(&p + 1));    //&P is the address. After + 1, it is still the address. If yes, the address is 4 / 8 bytes
									   //&p + 1 is the address of p + 1, which is the address after skipping the p variable in memory
	printf("%d\n", sizeof(&p[0] + 1)); //p[0] is a, & p[0] is the address of a, & p[0] + 1 is the address of b, and the address is 4 / 8 bytes
}

 

sizeof only focuses on the size of its memory space.

#include<string.h>
int main()
{
    char* p = "abcdef";
	printf("%d\n", strlen(p));			//p stores the address of 'a'. strlen(p) is to find the length of the string from the position of 'a', and the length is 6
	printf("%d\n", strlen(p + 1));   	//p+1 is the address of 'b'. Starting from the position of b, the length of the string is 5
	printf("%d\n", strlen(*p));			//*p is the first element a, the ASCII code value is 97, illegal access to memory, error!
	printf("%d\n", strlen(p[0]));		//P [0] < = = > * (P + 0) < = = > * P, an error is reported!
	printf("%d\n", strlen(&p));			//&p takes the address of p, but the end flag is unknown, random value
	printf("%d\n", strlen(&p + 1));	    //&P + 1 skips the address of the whole p, but the end flag is unknown, random value
	printf("%d\n", strlen(&p[0] + 1));  //P [0] - > * (P + 0) - > * P - > 'a', & P [0] is the address of the first character and & P [0] + 1 is the address of the second character
									    //From the position of the second character to the backward number character string, the length is 5
	                                    //Note:& P is the address of the variable p, and the memory of the variable p stores the address of the string
}

strlen is for string length, and also compares types!

Analyze the following codes:

int main()
{
	//Two dimensional array
	int a[3][4] = { 0 };
	printf("%d\n", sizeof(a));          //The array list is placed inside sizeof alone, and the size of the whole array is calculated
	printf("%d\n", sizeof(a[0][0]));    //Element of the first row and the first column, type int, 4 bytes
	printf("%d\n", sizeof(a[0]));       //a[0] represents the array name of the first row. a[0] is placed inside sizeof as an array list. It calculates the size of the first row, a total of 16 bytes
	printf("%d\n", sizeof(a[0] + 1));   //As the array name of the first row, a[0] does not meet the two special cases of sizeof, so a[0] represents the address of the first element, that is, the address of a[0][0]
										//a[0]+1 is the address of the second element in the first line, which is 4 / 8 bytes 
	printf("%d\n", sizeof(*(a[0] + 1)));  //The second element in the first line, type int, 4 bytes
	printf("%d\n", sizeof(a + 1));	    //A is the array name of the two-dimensional array. Based on the above analysis, a+1 is the address of the second line
										//Is an array pointer of type int(*)[4], and the address is 4 / 8 bytes
	printf("%d\n", sizeof(*(a + 1)));   //*(a+1) is the second row, which is equivalent to the array name of the second row, * (a+1) < = = > a [1],
										//sizeof(*(a + 1)) calculates the size of the second line, a total of 16 bytes
	printf("%d\n", sizeof(&a[0] + 1));  //a[0] is the address of the first line, & a[0] is the address of the first line, & a[0] + 1 is the address of the second line, 4 / 8 bytes
	printf("%d\n", sizeof(*(&a[0] + 1))); //*(& A [0] + 1) < = = > a [1], sizeof (a [1]) size is 16 bytes
	printf("%d\n", sizeof(*a));		    //A the array name of a two-dimensional array does not meet the two special cases of sizeof, * a is the first element of the two-dimensional array,
										//The first line. (*a<==>*(a+0)<==>a[0])
	printf("%d\n", sizeof(a[3]));       //It seems that a[3] is out of bounds, but it doesn't matter. sizeof(a[3]) deduces its one-dimensional array and its type,
										//a[3] is the array name in the fourth row, 4 int s, 16 bytes
	printf("%d\n", sizeof(&a+1));		//a is the address of the two-dimensional array, & a + 1 skips the address of the whole two-dimensional array (48 bytes are skipped), and the address is 4 / 8 bytes
	printf("%p\n", &a[0][0]);
	printf("%p\n", &a+1);

	printf("%p\n", a[0] + 1);
	printf("%p\n", a + 1);
	int* p = a + 1;
	return 0;
}

 

 

Summary:

Meaning of array name:

1. Sizeof (array name), where the array name represents the whole array, and the size of the whole array is calculated.

2. & array name, where the array name represents the whole array, and the address of the whole array is taken out. 3. In addition, all array names represent the address of the first element.

10. Pointer written test questions (selected written test questions of famous enterprises)

Written test question 1:

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

Output: 2,5

Parsing: * (a+1) is the second element 2 of the array, (& a+1) skips the entire array and is cast to (int *), * (ptr-1) is the fifth element 5 of the array

Written test question 2:

//Here, the size of the structure is 20 bytes
struct Test
{
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p;
//Suppose the value of p is 0x100000. What are the values of the expressions in the following table?
//It is known that the variable size of the structure Test type is 20 bytes
int main()
{
	printf("%p\n", p + 0x1);
	printf("%p\n", (unsigned long)p + 0x1);
	printf("%p\n", (unsigned int*)p + 0x1);
	return 0;
}

Output:

00000014
00000001
00000004

Analysis: p+0x1 is equivalent to skipping the whole structure, while the size of a structure is 20 bytes, and it is printed with "% p" (the compiler sets the printed address to hexadecimal (the address is originally binary, but can be expressed in 8 and hexadecimal), and 14 is converted to hexadecimal to 20);

(unsigned long) p computes p into an integer, p represents a number, and the integer + 1 is 00000001;

(unsigned int *) P converts p to integer pointer, integer pointer + 1, skip 1 integer, 4 bytes are 00000004

Item 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;
}

Output: 42000000

Item 4:

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

Output: 1

Written test question 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]);
    return 0;
}

Output: fffffc, - 4

Pen 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));
    return 0;
}

Output: 10,5

Written test question 7:

#include <stdio.h>
int main()
{
	char* a[] = { "work","at","alibaba" };
	char** pa = a;
	pa++;
	printf("%s\n", *pa);
	return 0;
}

Output: at

Written test question 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);
 printf("%s\n", *--*++cpp+3);
 printf("%s\n", *cpp[-2]+3);
 printf("%s\n", cpp[-1][-1]+1);
 return 0;
}

Output:

POINT
ER
ST
EW

 

Postscript:
● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!

-- By author: Xinxiao · old acquaintance

 

Keywords: C

Added by atokatim on Mon, 31 Jan 2022 13:17:16 +0200