当前位置:网站首页>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
边栏推荐
- UE像素流,来颗“减肥药”吧!
- Buildroot system for making raspberry pie cm3
- Weidongshan Internet of things learning lesson 1
- Affected tree (tree DP)
- Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
- 2020-05-21
- Arduino burning program and Arduino burning bootloader
- Shell script realizes the reading of serial port and the parsing of message
- 猜谜语啦(5)
- An enterprise information integration system
猜你喜欢
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
剑指 Offer 09. 用两个栈实现队列
Brief discussion on Buck buck circuit
【三层架构及JDBC总结】
Business modeling of software model | object modeling
Simple design description of MIC circuit of ECM mobile phone
MHA High available Cluster for MySQL
Stm32--- systick timer
【三层架构】
Example 009: pause output for one second
随机推荐
Lori remote control LEGO motor
Various types of questions judged by prime numbers within 100 (C language)
Affected tree (tree DP)
[three tier architecture and JDBC summary]
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
Xrosstools tool installation for X-Series
STM32 outputs 1PPS with adjustable phase
What are the test items of power battery ul2580
L298N module use
实例010:给人看的时间
STM32 single chip microcomputer -- volatile keyword
MATLAB skills (28) Fuzzy Comprehensive Evaluation
STM32 --- NVIC interrupt
How apaas is applied in different organizational structures
Go dependency injection -- Google open source library wire
Ble encryption details
STM32 --- GPIO configuration & GPIO related library functions
99 multiplication table (C language)
猜谜语啦(2)
STM32 lights up the 1.8-inch screen under Arduino IDE