当前位置:网站首页>Illustration of eight classic pointer written test questions

Illustration of eight classic pointer written test questions

2022-07-05 08:33:00 Life is made by oneself ~

The first question is

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 ?

a And ptr Memory map of :

 Insert picture description here
a + 1 and ptr - 1:

 Insert picture description here
After de quotation, the answer is :

2,5


The second question is

struct Test
{
    
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p = 0x100000;
// hypothesis p  The value of is 0x100000.  What are the values of the expressions in the following table ?
int main()
{
    
	printf("%p\n", p + 0x1);
	printf("%p\n", (unsigned long)p + 0x1);
	printf("%p\n", (unsigned int*)p + 0x1);
	return 0;
}

This problem involves the calculation of the size of the structure , If you have forgotten friends, you can have a look : Structure / Calculation of the size of the consortium
The size of this structure is 20 byte

The first thing to know about this problem is Pointer plus integer depends on pointer type , And integer plus integer can be added directly
p+0x1 Just skip one p Type size , Equivalent to +20
(unsigned long)p + 0x1 Add two integers , Direct addition
(unsigned int*)p + 0x1 skip p Type size , amount to +4

The final answer is :

0x100020
0x100001
0x100004


Third question

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

ptr1 Same as the first question , It should be noted that ptr2,(int)a + 1 Medium +1 Is to add a byte , An integer has four bytes ,a It's the first element address ,(int)a + 1 after :
 Insert picture description here
And because ptr2 yes int* type , So look back four bytes when dereferencing :
 Insert picture description here
After the small end storage is taken out, it will be 02 00 00 00
The final answer is :

4, 2000000(16 Hexadecimal does not print the previous 0)


Fourth question

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

There's a trap here : Comma expression
Real memory map :
 Insert picture description here

The final answer is :

1


Fifth question

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

a[4][2] It's easy to find , however p[4][2] Hard to find ,p The type of pointer is int(*)[4],p[4][2] Can be seen as :
 Insert picture description here
&p[4][2] - &a[4][2] The result is -4
 Insert picture description here
So the answer is :

FF FF FF FC , -4


Sixth question

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
answer :

10, 5


Question seven

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

 Insert picture description here
pa++ Backward pointing a Point to "at"
The answer for :

“at”


The eighth question

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

First draw the picture
 Insert picture description here
Because there is ++ and - -, Will change the result of the next step , So we should analyze step by step
1️⃣**++cpp
 Insert picture description here
Pointing to "POINT"

2️⃣*-- * ++cpp + 3
①++cpp
 Insert picture description here
②-- * ++cpp
 Insert picture description here
③*-- * ++cpp + 3
 Insert picture description here
Point to "ER"
3️⃣*cpp[-2] + 3
 Insert picture description here

 Insert picture description here Point to "ST"
4️⃣cpp[-1][-1] + 1

①cpp[-1]
 Insert picture description here
②cpp[-1][-1]
 Insert picture description here
③cpp[-1][-1] + 1
 Insert picture description here
Point to "EW"
So the answer is :

POINT
ER
ST
EW



原网站

版权声明
本文为[Life is made by oneself ~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050831349945.html