当前位置:网站首页>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
边栏推荐
- Management and use of DokuWiki
- U8g2 drawing
- 2022.7.4-----leetcode.1200
- 亿学学堂给的证券账户安不安全?哪里可以开户
- Five design details of linear regulator
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- Speech recognition learning summary
- Briefly talk about the identification protocol of mobile port -bc1.2
- Charge pump boost principle - this article will give you a simple understanding
- Arduino burning program and Arduino burning bootloader
猜你喜欢
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
MySQL MHA high availability cluster
猜谜语啦(8)
Sword finger offer 09 Implementing queues with two stacks
Xrosstools tool installation for X-Series
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Lori remote control LEGO motor
实例006:斐波那契数列
随机推荐
Sword finger offer 09 Implementing queues with two stacks
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Use indent to format code
Apaas platform of TOP10 abroad
[three tier architecture]
MATLAB小技巧(28)模糊综合评价
Stablq of linked list
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
第十八章 使用工作队列管理器(一)
猜谜语啦(9)
Old Wang's esp8266 and old Wu's ws2818 light strip
Infected Tree(树形dp)
【三层架构及JDBC总结】
Some pitfalls of win10 network sharing
Is the security account given by Yixue school safe? Where can I open an account
【NOI模拟赛】汁树(树形DP)
Keil use details -- magic wand
Five design details of linear regulator
OC and OD gate circuit
NTC thermistor application - temperature measurement