当前位置:网站首页>Pointer written test questions ~ approaching Dachang
Pointer written test questions ~ approaching Dachang
2022-07-06 03:23:00 【Little snail rushes towards】
Preface
author : Little snail rushes forward
quotes : I can accept failure , But I can't accept giving up

If you think the blogger's article is good , Please
give the thumbs-up , Collection , Follow and support bloggers . If you find any problems, you are welcome * Please make corrections in the comments section .
Catalog
Next, I will share some written questions about pointer , I hope you can get something .
Pen test 1:
#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;
}
What is the result of this problem ?
For pointer type topics , Our best way to solve the problem is to draw pictures , I will use pictures to solve all the following pointer questions .
a Distribution diagram of array elements

As we can see from the picture The pointer ptr Pointing to Array a Other elements .
a It's an array name --> The address of the first element .
Then we (a+1) Skip an element from the first address , Point to the address of the next element ,*(a+1) You find the element 2.
that (ptr-1) Move left by a reshaped size , Point to 5 This element ,*(ptr-1) Find this element .
So our The result is 2,5.

Pen test 2
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
// hypothesis p The value of is 0x000000. What are the values of the expressions in the following table ?
// It is known that , Structure Test The variable size of type is 20 Bytes
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}First We need to understand p What is it? ,p It's a pointer to a structure ,%p Value is the address of the printed pointer , We can assume that p The address for 0x000000, that p+0x1 What does that mean ? It's really just the p+20-->0x000014.
among (unsigned long)p, Put the pointer p Force the type to be converted to an integer , therefore (unsigned long)p+0x1-->0x000001.
secondly (unsigned int*)p, This affects The pointer +- Number of bytes skipped , among ungsing int* The size is 4 byte , therefore (unsigned long)p + 0x1-->0x000004.
Pen test 3
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;
}
a Memory distribution diagram of array

Here we first understand clearly ,ptr1 and ptr2 All are int * Pointer to type .
(&a + 1) Represents skipping the entire array , Point to the element behind the array .
(int)a take a It's plastic surgery ,(int )a+1 Express a The value of the add 1, Just The pointer jumps over 1 Bytes ( Position as shown in the figure ).
%x Refers to printing in hexadecimal .
Well, we understand the meaning of these variable names , Let's start solving problems .
ptr1[-1]- ->*((&a+1)-1), That is, dereference the pointer to the last position of the array , Just Found the element 0x4.
*ptr2, Is to find ptr2 The element that the pointer points to , According to the memory access rules , To pour this out -->02 00 00 00 , So the final value printed on the screen is 2 00 00 00.
Pen test 4
#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;
}
a Distribution diagram of array elements

This topic is actually a pit , People mistakenly think that initialization is
0 1
2 3
4 5
This form , Actually () It's a , The sign expression , The last value in the right bracket of the output value . So according to the array element distribution diagram above , Our output p[0]-->*(p+0) The value of is .
Pen test 5
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 Element distribution diagram of array

Here we understand a What is it? ?p What is it again? ?&p[4][2] and &a[4][2] What does it mean ?
among a It's a two-dimensional array ,a The type of int(*)[4],p For array pointers ,p The type of int(*)[4].
&a[4][2], The yellow square in the figure is a[4][2] Pointing elements .
p[4][2]-->&*(*(p+4)+2), First P+4 Point to the position in the diagram , Why? ? People may have questions , because p Think you point to 4 individual int type , So every time +1 Will skip 4 A plastic element . secondly , stay (*(p+4)+2),*(p+4) Will visit 4 An integer array , Array name + I'll skip it 2 Elements , Point to 3 Elements , stay *(*(p+4)+2) You'll find number one 3 Elements ( The green square in the picture ).
therefore , &p[4][2] - &a[4][2]( The pointer - The pointer )= The number of elements between pointers 4, Because it is the pointer to the address - The pointer result of the high address is -4.
-4
Original code :10000000 00000000 00000000 00000100
Inverse code :11111111 11111111 11111111 11111011
Complement code :1111 1111 1111 1111 1111 1111 1111 1100
Hexadecimal :F F F F F F F C
%p Print FFFFFFFC.
%d Print -4.
Pen test 6
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;
}
aa Element distribution diagram of array

This question and the written examination question 1 And its similar , It's easy to solve the problem after drawing .
ptr1-1--> Point to aa[1][4],*(prt1-1) find 10.
ptr2-1--> Point to aa[0][4],*(ptr2-1)z find 5.
Pen test 7
#include <stdio.h>
int main()
{
char *a[] = {"work","at","alibaba"};
char**pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}a And pa Memory distribution
Here we also draw good pictures ,pa It's a secondary pointer ,a Is a constant string , among a The variable name of represents the address of the first element , Start pa-->w, later pa++-->a, therefore &s Print out ''at''.
Now let's officially enter our final topic , Are you ready for the challenge ?
Pen test 8
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;
}
Initial memory layout

After we draw the initial memory layout , Start solving problems .
1**ccp, We can clearly see from the figure that it points to a string FIRST(cpp-->c+2).
2*--*++cpp+3( Be careful ++-- Are the values of the change quantity ), Here we first operate close to the operator of the variable ,++cpp, This makes cpp Point to c+1,*(c+1) find c+1, stay --*++cpp refer to c+1-->c,*--*++cpp Find the string ENTER The first address , then ,*--*++cpp+3, find E The address of , Print ER.(cpp-->c+1,c+1-->c).
3*cpp[-2]+3-->*(cpp-2)+3, here cpp-2 bring cpp Point to c+3,*(c+3) find FIRST The first address ,*(cpp-2)+3 find S The first address , Print ST.(cpp-->c+3)
4cpp[-1][-1]+1-->*(*(cpp-1)-1)+1,cpp-1 Point to c+2,*(cp-1)-1-->( Point to )c+1,*(*(cpp-1)-1) find NEW The first address ,*(*(cpp-1)-1)+1 find E The address of , Print EW.
summary
1 It is very important to draw and analyze when doing pointer type topics .
2p[1] Can be equivalent to *(p+1),p[1][1] It can also be equivalent to *(*(p+1)+1).
3 Remember that the array name is the address of the first element ( I won't say more about the two special cases ).
4 The pointer - Pointer means the number of elements between two pointers .
If you like it, just point a compliment to support the blogger !

边栏推荐
- Analyze 菜单分析
- Getting started with applet cloud development - getting user search content
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- Game theory matlab
- 这些不太会
- 【SLAM】ORB-SLAM3解析——跟踪Track()(3)
- Force buckle 1189 Maximum number of "balloons"
- How to choose PLC and MCU?
- Idea push rejected solution
- ERA5再分析资料下载攻略
猜你喜欢

Force buckle 1189 Maximum number of "balloons"

指针笔试题~走近大厂

js凡客banner轮播图js特效

Jenkins basic knowledge ----- detailed explanation of 03pipeline code

Explore pointers and pointer types in depth

Recommended foreign websites for programmers to learn

ESBuild & SWC浅谈: 新一代构建工具

Esbuild & SWC: a new generation of construction tools

Image super-resolution using deep convolutional networks(SRCNN)解读与实现

施努卡:3d视觉检测应用行业 机器视觉3d检测
随机推荐
NR modulation 1
SWC introduction
Performance analysis of user login TPS low and CPU full
Résumé des méthodes de reconnaissance des caractères ocr
3857 Mercator coordinate system converted to 4326 (WGS84) longitude and latitude coordinates
Leetcode problem solving -- 98 Validate binary search tree
three.js网页背景动画液态js特效
SD卡報錯“error -110 whilst initialising SD card
【概念】Web 基础概念认知
Linear programming matlab
ArabellaCPC 2019(补题)
深度解析指针与数组笔试题
js凡客banner轮播图js特效
Differences and application scenarios between resulttype and resultmap
JS regular filtering and adding image prefixes in rich text
jsscript
施努卡:3d视觉检测应用行业 机器视觉3d检测
Yyds dry inventory what is test driven development
EDCircles: A real-time circle detector with a false detection control 翻译
Item 10: Prefer scoped enums to unscoped enums.