当前位置:网站首页>Dachang classic pointer written test questions
Dachang classic pointer written test questions
2022-07-07 20:19:00 【On the Pearl River】
This part is the classic pointer question , Practice hard , If you refuse, you will , As soon as I write fei
Catalog :
Pointer classic question
1. This topic is the epitome of classic pointer pen test questions
2. drawing 、 drawing 、 drawing
3. Classic questions :
Classic questions :
Pen test 1:
Particular attention :
①int* ptr = (int*)(&a + 1);
//&a It takes out the address of the entire array , therefore &a The type is int(*)[5], This type of +-1 What I'm skipping is 5 individual int type
// Cast , The type changes , And the value remains the same
②printf("%d,%d", *(a + 1), *(ptr - 1));
//*(ptr-1) Why just skip 1 individual int type , because ptr The type is int*, This type of +-1 skip 1 individual int type
if int* p=&a;* explain p It's a pointer variable ,int explain p The object is int type .p The type is int*
Answer to this question :2,5
The code is as follows :
#include <stdio.h>
int main()
{
int a[5] = { 1,2,3,4,5 };
int* ptr = (int*)(&a + 1);
printf("%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
Pen test 2:
Particular attention :
there 0x1 It's hexadecimal , Convert to decimal to 1
It is known that p The value of is 0x100000,
①printf("%p\n", p + 0x1);
//p Is a structure type pointer variable ,p+-1 It means skipping a structure type size
if p Is an integer pointer variable ,p+-1 It means skipping an integer size
②printf("%p\n", (unsigned long)p + 0x1);
(unsigned long)p,p The type of changes but the value does not change ,
p The value of is 0x100000, convert to unsigned int Type followed by 1048576,+1 by 1048577, The address of this value is 0x100001
③printf("%p\n", (unsigned int*)p + 0x1);
// if p It's an integer pointer ,p+-1 Represents skipping an unsigned integer size
Answer to this question :
0x100014,0x100001,0x100004
The code is as follows :
#include <stdio.h>
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
Pen test 3:
Particular attention :
①int* ptr2 = (int*)((int)a + 1);
//a Is the address of the first element of the array , Force type to int type , The value remains the same , The type changes
// if a=0x0012ff40, be a+1=0x0012ff44,(int)a+1=0x0012ff41
The storage of the array in memory ,vs Adopt small end storage mode ,0x00 00 00 01 Store in memory as 01 00 00 00
②printf("%x,%x", ptr1[-1], *ptr2);
//ptr2 The type is int*, Dereference operation access 1 Integer sizes , Then print in hexadecimal
Answer to this question :4,2000000
The code is as follows :
#include <stdio.h>
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;
}
Pen test 4:
Particular attention :
①int a[3][2] = { (0,1),(2,3),(4,5) };
// Here is the comma expression , Note that (), No { } , See the figure below to see the difference !
Answer to this question :1
The code is as follows :
#include <stdio.h>
int main()
{
int a[3][2] = { (0,1),(2,3),(4,5) };
int* p;
p = a[0];
printf("%d", p[0]);
return 0;
}
Pen test 5:
Particular attention :
①int a[5][5];
// In a two-dimensional array int a[5][5] in ,a Is the address of the first element of the array , The type is int (*)[5];
②int(*p)[4];
//p The array pointed to has 4 Elements , Each for int type ,+1 skip 1 individual (4 individual int An array of types )
③p = a;
int (*)[4]=int (*)[5];
printf("%p\n", &p[4][2] - &a[4][2]);
//&p[4][2] and &*(*(p+4)+2) equally .p Is an array pointer ,+4 skip 4 individual (4 individual int An array of types )//p++ It is the green icon in the above figure , Orange is p[4] Quoting
④&p[4][2] - &a[4][2]=-4;
//-4 Press %p Print , Yes, it will -4 The complement of is converted to hexadecimal
Original code :
10000000 00000000 00000000 00000100
Inverse code :
11111111 11111111 11111111 11111011
Complement code :
11111111 11111111 11111111 11111100
obtain 0xFF FF FF FC
Answer to this question :FF FF FF FC,-4
The code is as follows :
#include <stdio.h>
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;
}
Pen test 6:
Particular attention :
①int* ptr1 = (int*)(&aa + 1);
//&aa What you get is the address of the entire array ,+1 Skip the entire 2D array
②int* ptr2 = (int*)(*(aa + 1));
//aa Is the address of the first row of the two-dimensional array ,
*(aa + 1)=aa[1],aa[1] Is the address of the first element in the second line , This type is int*, So here's (int*) redundant
Answer to this question :10,5
The code is as follows :
#include <stdio.h>
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;
}
Pen test 7:
Particular attention :
①char* a[ ] = { "work","at","alibaba" };
// This array stores work、at、alibaba The address of the first element
②char** pa = a;
//pa Pointing to a The second element of the array , After dereferencing, we get at The address of the first element
③printf("%s\n", *pa);
//printf When printing ,%s According to the address of the first element , Print backwards , encounter \0 stop it
Answer to this question :at
The code is as follows :
#include <stdio.h>
int main()
{
char* a[] = { "work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
Pen test 8:
This topic is the epitome of pointer pen test questions
Particular attention :
Code analysis :
++cpp after cpp The address pointed to will change
①printf("%s\n", **++cpp);
//++cpp after ,cpp Point to c+2, After the first dereference, we get c+2, and c+2 Pointing to the array c The third element , The array is obtained after the second dereference c The third element “POINT” The address of the first character ,printf When printing ,%s According to the first character P Print the address backwards , encounter \0 stop it
②printf("%s\n", *--*++cpp+3);
//++cpp after ,cpp Point to c+1, After the first dereference, we get c+1,c+1 Again -1 Pointing to the array c First element , The array is obtained after the second dereference c First element "ENTER" The address of the first character ,+3 obtain T The address of ,printf When printing ,%s According to the characters T Print the address backwards , encounter \0 stop it
③printf("%s\n", *cpp[-2]+3);
//**(cpp-2)+3;cpp-2 Point to c+3, After the first dereference, we get c+3, The address points to the array c Fourth element , The array is obtained after the second dereference c Fourth element "FIRST" The address of the first character ,+3 obtain R The address of ,printf When printing ,%s According to the characters R Print the address backwards , encounter \0 stop it
④printf("%s\n", cpp[-1][-1]+1);
//cpp[-1][-1]+1 and *(*(cpp-1)-1)+1;cpp-1 Point to c+2, After the first dereference, we get c+2, The address -1 Pointing to the array c The second element , The array is obtained after the second dereference c The second element "NEW" The address of the first character ,+1 obtain E The address of ,printf When printing ,%s According to the characters E Print the address backwards , encounter \0 stop it
Answer to this question :POINT,ER,ST,EW
The code is as follows :
#include <stdio.h>
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;
}
Conclusion :
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
边栏推荐
- Traversal of Oracle stored procedures
- How C language determines whether it is a 32-bit system or a 64 bit system
- ASP.NET学习& asp‘s one word
- Force buckle 459 Duplicate substring
- 关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
- Some important knowledge of MySQL
- 力扣 88.合并两个有序数组
- 如何在软件研发阶段落地安全实践
- With st7008, the Bluetooth test is completely grasped
- The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
猜你喜欢
ASP.NET学习& asp‘s one word
How to cooperate among multiple threads
With st7008, the Bluetooth test is completely grasped
力扣 2319. 判断矩阵是否是一个 X 矩阵
网络原理(1)——基础原理概述
Machine learning notes - explore object detection datasets using streamlit
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
[MySQL - Basic] transactions
Force buckle 2319 Judge whether the matrix is an X matrix
随机推荐
Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
Vulnhub tre1
Mongodb由浅入深学习
[résolution] le paquet « xxxx» n'est pas dans goroot
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Solve the problem of incomplete display around LCD display of rk3128 projector
Oracle 存储过程之遍历
华为CE交换机下载文件FTP步骤
CJSON内存泄漏的注意事项
Traversée des procédures stockées Oracle
【解决】package ‘xxxx‘ is not in GOROOT
ASP. Net learning & ASP's one word
力扣 643. 子数组最大平均数 I
kubernetes之创建mysql8
sql 常用优化
力扣 1961. 检查字符串是否为数组前缀
Kubernetes——kubectl命令行工具用法详解
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
力扣 1037.有效的回旋镖
Meta Force原力元宇宙系统开发佛萨奇模式