当前位置:网站首页>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 !
边栏推荐
- 【RISC-V】外部中断
- Performance test method of bank core business system
- How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
- Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- Problems encountered in 2022 work IV
- Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
- Linear regression and logistic regression
- Descriptor implements ORM model
- 3.2 rtthread 串口设备(V2)详解
猜你喜欢
Explore pointers and pointer types in depth
MPLS experiment
Software design principles
Analyze menu analysis
Idea push rejected solution
My C language learning record (blue bridge) -- on the pointer
Four logs of MySQL server layer
svg拖动点裁剪图片js特效
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
指针笔试题~走近大厂
随机推荐
Mysqldump data backup
出现Permission denied的解决办法(750权限谨慎使用)
ArabellaCPC 2019(补题)
JS regular filtering and adding image prefixes in rich text
1.16 - 校验码
蓝色样式商城网站页脚代码
Arabellacpc 2019 (supplementary question)
3.1 rtthread 串口设备(V1)详解
指针笔试题~走近大厂
Pytorch基础——(1)张量(tensor)的初始化
SD卡報錯“error -110 whilst initialising SD card
Era5 reanalysis data download strategy
SWC介绍
Résumé des méthodes de reconnaissance des caractères ocr
Overview of OCR character recognition methods
遥感图像超分辨重建综述
OCR文字识别方法综述
Differences and application scenarios between resulttype and resultmap
Data and Introspection__ dict__ Attributes and__ slots__ attribute
NR modulation 1