当前位置:网站首页>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 ~】
List of topics
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 :

a + 1 and ptr - 1:
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 :
And because ptr2 yes int* type , So look back four bytes when dereferencing :
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 :
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 :
&p[4][2] - &a[4][2] The result is -4
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;
}

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

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 
Because there is ++ and - -, Will change the result of the next step , So we should analyze step by step
1️⃣**++cpp
Pointing to "POINT"
2️⃣*-- * ++cpp + 3
①++cpp
②-- * ++cpp
③*-- * ++cpp + 3
Point to "ER"
3️⃣*cpp[-2] + 3
Point to "ST"
4️⃣cpp[-1][-1] + 1
①cpp[-1]
②cpp[-1][-1]
③cpp[-1][-1] + 1
Point to "EW"
So the answer is :
POINT
ER
ST
EW
边栏推荐
- 剑指 Offer 05. 替换空格
- STM32 --- serial port communication
- 实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
- Sizeof (function name) =?
- Infected Tree(树形dp)
- STM32 --- NVIC interrupt
- Installation and use of libjpeg and ligpng
- Chapter 18 using work queue manager (1)
- Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
- Cinq détails de conception du régulateur de tension linéaire
猜你喜欢

Sword finger offer 06 Print linked list from end to end

【三层架构及JDBC总结】

QEMU STM32 vscode debugging environment configuration

UE像素流,来颗“减肥药”吧!

Matlab tips (28) fuzzy comprehensive evaluation

UE pixel stream, come to a "diet pill"!

猜谜语啦(7)

Briefly talk about the identification protocol of mobile port -bc1.2

Summary of SIM card circuit knowledge

Lori remote control commissioning record
随机推荐
Low code platform | apaas platform construction analysis
Example 007: copy data from one list to another list.
Count the number of inputs (C language)
leetcode - 445. Add two numbers II
Negative pressure generation of buck-boost circuit
Various types of questions judged by prime numbers within 100 (C language)
Installation and use of libjpeg and ligpng
Stablq of linked list
【NOI模拟赛】汁树(树形DP)
Five design details of linear regulator
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
猜谜语啦(7)
[three tier architecture]
696. 计数二进制子串
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Circleq of linked list
Buildroot system for making raspberry pie cm3
UE pixel stream, come to a "diet pill"!
实例008:九九乘法表
Classic application of MOS transistor circuit design (2) - switch circuit design