C language --- pointer written test questions

Keywords: C Back-end

Question 1:

(& A) gets an array pointer such as int(*)[5]. Then + 1 skips the whole array and points to the position after 5.

*(a+1)=a[1]=2;    * (ptr-1) = skip an element to point to 5

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

  Question 2:

1. The pointer + 1 skips an element, and the current element is a structure. A structure of 20 bytes, converted into hexadecimal is 14     0x100014

2. Originally, p is struct Test *, but the pointer of the whole structure is strongly converted to (unsigned long) p, which is converted to an integer. Integer + 1 is a simple integer + 1

3. Convert to int * type, and then + 1 skips an element + 4

struct Test
{
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p = (struct Test*)0x100000;

int main(){
	printf("%p\n", p + 0x1);
	printf("%p\n", (unsigned long)p + 0x1);
	printf("%p\n", (unsigned int*)p + 0x1);

Question 3:

&A gets an int (*) [4]. If the array pointer performs the + 1 operation, skip the entire array element and point to the position after 4.

a is the array name, cast type conversion (int), and the first element address (int) of the array is obtained. The subsequent + 1 is int+1;

(int) a is implicitly converted to (int*)a pointing to the first element of the array, and then forcibly converted to int

1 is an int, which contains four bytes. Suppose a is 0x100, a+1 is 0x101;

ptr1[-1] is * (ptr1-1); it is 4

Dereference operation:

1. Find the corresponding memory according to the address stored in it.

2. Decide how much space to find according to the type of pointer.

*ptr2

How are 1 and 2 stored in specific memory?

00 00 01, because of the problem of byte order, 01 00 00     00 00 00 02     02 00 00 00

An int has four bytes. Starting from 0x101, you can find the last three bytes of element 1 and the first byte of element 2, 00 022000000

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)

 

Question 4:

The final value of comma expression is the value after the last comma. Therefore, in the following code, the final valid number is

1,3,5;

int main(){
	int a[3][2] = { (0, 1), (2, 3), (4, 5) };
	int* p;
	p = a[0];
	printf("%d", p[0]);

1

Question 5:

This assignment operation itself is unscientific (int(*)[4] and int (*) [5] are two different types. There is a great risk of forcibly assigning two different types of pointers)

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]);

The decimal system is: & P [4] [2] - [a [4] [2] = - 4

1000 million 0100 source code

1111 1111 1111 1111 1111 1111 1111 1011 inverse code

1111 1111 1111 1111 1111 1111 1111 1100 inverse code + 1 = complement code

 F       F      F      F      F      F      F       C

Hex: fffffc

Pointer subtraction: it is to calculate how many elements are separated between two pointers!

 

Question 6:

  (& AA + 1) = > int (*) [2] [5]; array pointer to two-dimensional array; position after 10;

*(aa+1) = aa[1]; point to the first element address of the second line.

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));

Question 7:

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

 

  Question 8:

cpp: 0x2000

++cpp: 0x2004

*++cpp: 0x1008

**++cpp: 0x300

%s: Print string, 0x300 corresponds to POINT

Now cpp is 0x2004

++cpp: 0x2008

*++cpp: 0x1004

--0x1004: 0x1000

*0x1000: 0x100

0x100+3: since 0x100 is char *, 0x103 starts printing until \ 0

ER

ENTER

Now cpp is 0x2008

cpp[-2] is (0x2008-2) and then dereference

0x2008-2=>0x2000

*0x2000=0x100c

*0x100c=0x400

0x400+3 = since 0x400 is char *, 0x403 starts printing until \ 0

​​​ST

FIRST

The current cpp is 0x2008

cpp[-1]=0x2004 re dereference 0x1008

0x1008[-1]

0x1004 re dereference 0x200

0x200+1=0x201

EW

NEW

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

 

 

 

Posted by scheibyem on Mon, 25 Oct 2021 05:54:05 -0700