Pointer written test questions

Posted by amazing on Sun, 23 Jan 2022 20:47:16 +0100

Question 1:

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

Resolution:

1.a has five elements: 1, 2, 3, 4 and 5

2. Take out the address of a + 1, skip the entire array, and then forcibly convert it to the address of the first element of int *

3. Therefore, the address of * (a+1) a is the address of the first element, and + 1 is the address of the second element

4.(*ptr-1) is the first address of the next array, and - 1 gets the address of the last element of the array

So the final result is 2.5

Question 2:

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()
{
 p=(struct Test *)0x100000;
 printf("%p\n", p + 0x1);
 printf("%p\n", (unsigned long)p + 0x1);
 printf("%p\n", (unsigned int*)p + 0x1);
 return 0;
}

Resolution:

1.*p is the pointer variable of the structure

2.p is pointing to structure + 1, skipping 20 bytes, 0x100014 (pay attention to hex conversion)

3. First convert p to long integer + 1, that is, add an integer 1 0x100001

4. First convert p to an int* +1, which is equivalent to adding 4 0x100004

Note:% p is a hexadecimal number printed in the form of address, not an address. 0 before the most significant bit

% x is printed in hexadecimal, and the 0 before the highest bit will be omitted.

Question 3:

int main()
{
    int a[4] = { 4,3,2,1 };
    int *ptr1 = (int *)(&a + 1);
    int *ptr2 = (int *)((int)a + 1);
    printf( "%x,%x", ptr1[-1], *ptr2);
    return 0;
}

Resolution:

1.ptr1 first takes out the address of a, + 1 skips the entire array, and then forcibly converts this address to int*

2.ptr2 first converts the address of the first element of a to int plus the number 1, and then converts it to int *, which is equivalent to skipping a byte

3. Print: ptr1[-1] is equivalent to * (ptr1 + (- 1) the result jumps back to 4

4. Print: ptr2 jumps only one byte, so the result is 3 00

Note: the form of small end storage is that the low weight bit is at the low address and the high weight bit is at the high address, but it should be obtained according to the normal sorting. 03 is the highest address of this shaping, so 0x3 0 00 is obtained

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

Resolution:

1. First, the comma expression 0 is stored in the array. 1 takes the final value as 1, 2, 3 takes the final value as 3, 4, 5 takes the final value as 5

2. Put the address of the first element of the array into * p

3 print: the value of p[0] is equivalent to * (p+0) is 1

be careful:

(0, 1) this form is a comma expression. It is not an array element {} but an array element

Question 5:
 

#include <stdio.h>
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;
}

Resolution:

1.int a is an array of five rows and five columns

2.int *p is an array pointer that can point to four elements. (* p) represents a pointer, which points to an integer with four elements

(address)

3.p equals a, which is equivalent to p pointer pointing to a (skipping four elements at a time)

4. & P [4] [2] is equivalent to * (* (P + 4) + 2), & A [4] [2] is equivalent to (* (* a+4)+2)

5. So% d is printed as - 4, and% p is printed in hexadecimal in the form of address, so - 4 should be converted to hexadecimal

6 is FF FC

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

Resolution:

1.ptr1 is forced to convert the address after skipping the entire array to int*

2.ptr2 is to skip the address of the first line, dereference the address of the first element of the second line, and forcibly convert it to int*

3.ptr1 -1 gets the address of the last element, and the dereference is equal to 10

4.ptr2-1 obtains the address of the last element in the first line, and the dereference is equal to 5

5. So output 10 and 5

Question 7

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

Resolution:

1. Char * a [] A is first combined with the block to represent an array, and then combined with the pointer to represent a pointer to represent a pointer array

2. Put the address of w in pa

3.pa + + skip an element

4. Print as at

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

1.c is an array before [] and a pointer (pointer array) before * in combination

Stored is the address of e, the address of n, the address of p, and the address of f

2.char **cp [] indicates that the pointer of char * type is stored

Respectively point to the address of f, the address of p, the address of n, and the address of c

3.char *** cpp refers to the address of the first element of cp

4.**++cpp,cpp first + + gets the second element, dereference gets c+2, dereference gets the address of p, and% s prints back to get point

5.*--*++cpp+3 first + + gets the third element, dereference gets c+1, and then -- c+1 becomes c, dereference gets the address of e, add three to get the address of e, and print it back% s

6.*cpp[-2]+3 cpp[-2] is equivalent to * (cpp-2) get the content of the first element, dereference to get the address of f + 3, get the address of s, and print st backward

7.cpp[-1][-1]+1 cpp[-1][-1] is equivalent to * (* (cpp-1)-1) cpp-1 obtains the address of the second element, * obtains the content of CPP c+2, then - 1 is c+1, the address of dereference is n, + 1 is the address of e, and is printed backward as ew

Note that + + -- will actually change the original content, and the expression will not change the original content

Topics: C