当前位置:网站首页>图解八道经典指针笔试题
图解八道经典指针笔试题
2022-07-05 08:31:00 【命由己造~】
第一题
int main()
{
int a[5] = {
1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( "%d,%d", *(a + 1), *(ptr - 1));
return 0; }
//程序的结果是什么?
a与ptr的内存图:
a + 1和 ptr - 1:
解引用后答案为:
2,5
第二题
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p = 0x100000;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
本题涉及结构体大小的计算,有遗忘的小伙伴可以看一看:结构体/联合体大小的计算
此结构体的大小为20字节
这道题首先要知道指针加整数要看指针类型,而整数加整数就可以直接加p+0x1
就是跳过一个p类型大小,就相当于+20(unsigned long)p + 0x1
俩整数相加,直接加(unsigned int*)p + 0x1
跳过p类型大小,相当于+4
最后答案为:
0x100020
0x100001
0x100004
第三题
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跟第一题一样,要注意的是ptr2,(int)a + 1中的+1是加一个字节,而一个整形有四个字节,a是首元素地址,(int)a + 1后:
而因为ptr2是int* 类型,所以解引用时要向后看四个字节:
小端存储取出来后就为02 00 00 00
最后的答案为:
4, 2000000(16进制不打印前面的0)
第四题
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf( "%d", p[0]);
return 0;
}
这里有个陷阱:逗号表达式
真正的内存图:
最后的答案为:
1
第五题
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]
很好找,但是p[4][2]
不好找,p指针的类型是int(*)[4]
,p[4][2]
可以看做:&p[4][2] - &a[4][2
]结果是-4
所以答案为:
FF FF FF FC , -4
第六题
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;
}
答案:
10, 5
第七题
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
pa++
后指向的a指向"at"
答案为:
“at”
第八题
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️⃣**++cpp
指向的是"POINT"
2️⃣*-- * ++cpp + 3
①++cpp
②-- * ++cpp
③*-- * ++cpp + 3
指向的为"ER"
3️⃣*cpp[-2] + 3
指向的为"ST"
4️⃣cpp[-1][-1] + 1
①cpp[-1]
②cpp[-1][-1]
③cpp[-1][-1] + 1
指向的为"EW"
所以答案为:
POINT
ER
ST
EW
边栏推荐
- My-basic application 1: introduction to my-basic parser
- Soem EtherCAT source code analysis II (list of known configuration information)
- 99 multiplication table (C language)
- STM32 single chip microcomputer - bit band operation
- Sword finger offer 06 Print linked list from end to end
- 每日一题——输入一个日期,输出它是该年的第几天
- Chapter 18 using work queue manager (1)
- 2022.7.4-----leetcode.1200
- STM32 --- configuration of external interrupt
- STM32---IIC
猜你喜欢
Sword finger offer 09 Implementing queues with two stacks
Arduino operation stm32
STM32 single chip microcomputer -- volatile keyword
MATLAB小技巧(28)模糊綜合評價
319. 灯泡开关
QEMU STM32 vscode debugging environment configuration
Management and use of DokuWiki (supplementary)
Ble encryption details
实例010:给人看的时间
实例008:九九乘法表
随机推荐
Sword finger offer 09 Implementing queues with two stacks
动力电池UL2580测试项目包括哪些
STM32 tutorial triple ADC interleaved sampling
实例010:给人看的时间
第十八章 使用工作队列管理器(一)
Array integration initialization (C language)
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Installation and use of libjpeg and ligpng
leetcode - 445. Add two numbers II
go依赖注入--google开源库wire
Esphone retrofits old fans
UE像素流,来颗“减肥药”吧!
Agile project management of project management
STM32 single chip microcomputer -- volatile keyword
剑指 Offer 09. 用两个栈实现队列
QEMU STM32 vscode debugging environment configuration
Arduino+a4988 control stepper motor
Low code platform | apaas platform construction analysis
Google sitemap files for rails Projects - Google sitemap files for rails projects
Use indent to format code