Some topics about pointers in C language (2)

This article continues the previous blog

Some topics of C language pointer (1)_Naion's blog-CSDN blog https://blog.csdn.net/Naion/article/details/122548523 Let's continue to do some topics.

 

Written test 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;
}
//程序的结果是什么?

Parse:

2.a is the address of the first element of the array, +1 will get the address of the second element of the array, in *, you will get the element 2;

5.&a, get the address of the array, and then +1, skip the entire array. After the coercion, the position of ptr is behind the array. -1, it points to 5, that is, after it -1 is the address of 5, in *, you will get 5;

Written Exam Question 2:

struct Test
{
     int Num;
     char *pcName;
     short sDate;
     char cha[2];
     short sBa[4];
}*p;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
//已知,结构体Test类型的变量大小是20个字节
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;
}

Parse:

Here, we assume that the size of this structure is 20 bytes, and tell the p value to be 0x100000. Since it is assumed, when running, the results may be different, but their difference is the same.

0x100014.p here is the address of the structure, it is +1, that is to skip the size of the entire structure by 20 bytes, and then convert 20 to hexadecimal, which is 0x100014;

0x100001.p is forced to be converted to an unsigned long integer, and then +1, which is its value +1, which is 0x100000;

0x100004.p is coerced into an unsigned int pointer, and then +1 will skip the size of this int pointer, which is 0x100004;

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

Parse:

4.&a, is the address of this array, and then +1, skip this array, where ptr1[-1], written in the form of a pointer is *(ptr1 - 1), which is 4;

2000000 (little endian mode) a, is the address of the first element of the array, then (int), force the type to be converted to decimal, +1, that is to add 1 to the decimal, and then convert to hexadecimal. Note that int is a skip 4 bytes.

Written Question 4:

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

 Parse:

 ff ff ff fc. As shown in the figure, you can convert -4 to hexadecimal.

-4. As shown.

Written Question 5:

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

Parse:

1. Pay attention to the parentheses here, not {}, this is a comma expression, so the elements of the entire two-dimensional array are 1, 3, 5. p=a[0], get the first row, and then p[0 ], which is the first element of the first row;

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

Parse:

10.&aa get the address of the two-dimensional array, then +1, skip the two-dimensional array, convert it to int* type, and use the ptr1 pointer to receive.ptr1-1 is the address of element 10, and then * is the element 10;

5.aa is the address of the first element of the array. For a two-dimensional array, it is its first line. Then +1, skip the first line, and come to the second line. This address is received by the ptr2 pointer.-1, It is the address of 5, and then *, the element 5 is taken out;

Written Exam Question 7:

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

Parse:

at. As shown in the figure:

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

Parse:

 

 The pointer points to the picture: (pay attention to the priority!)

POINT.cpp++, to c+2, and then *, to get the value POINT inside;

ER.cpp inherits the address of the previous print, then ++, to the address of c+1, then *, to get the address of c+1, and then --, indicating that c+1 is changed to c, that is to say , its point is not NEW, but ENTER, +3, it reaches the address of E, and then *, it is ER;

ST.*cpp[-2]+3, it is equivalent to * *(cpp-2)+3.cpp-2, it goes to c+3, then *, it gets the address of c+3, then * , then get FIRST, and then +3, when you get to S, take out ST;

EW. Equivalent to *(*(cpp-1)-1)+1, -1, then you get to c+2, then *, you get c+2, and then -1, then c+2 becomes c +1, then *, you will get NEW, and then +1, you will get EW;

Guess you like

Origin blog.csdn.net/Naion/article/details/122590308