当前位置:网站首页>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
边栏推荐
- 实例009:暂停一秒输出
- Anonymous structure in C language
- FIO测试硬盘性能参数和实例详细总结(附源码)
- go依赖注入--google开源库wire
- STM32 outputs 1PPS with adjustable phase
- MHA High available Cluster for MySQL
- Old Wang's esp8266 and old Wu's ws2818 light strip
- Stm32--- systick timer
- Example 007: copy data from one list to another list.
- UE像素流,来颗“减肥药”吧!
猜你喜欢
【三层架构及JDBC总结】
Low code platform | apaas platform construction analysis
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
[three tier architecture]
猜谜语啦(10)
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
猜谜语啦(7)
剑指 Offer 05. 替换空格
实例007:copy 将一个列表的数据复制到另一个列表中。
Charge pump boost principle - this article will give you a simple understanding
随机推荐
STM32 single chip microcomputer - external interrupt
319. 灯泡开关
Example 010: time to show
每日一题——输入一个日期,输出它是该年的第几天
Arrangement of some library files
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
go依赖注入--google开源库wire
NTC thermistor application - temperature measurement
猜谜语啦(11)
实例009:暂停一秒输出
How to write cover letter?
Chapter 18 using work queue manager (1)
關於線性穩壓器的五個設計細節
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
实例010:给人看的时间
Briefly talk about the identification protocol of mobile port -bc1.2
STM32 --- GPIO configuration & GPIO related library functions
leetcode - 445. Add two numbers II
Use indent to format code
How to copy formatted notepad++ text?