当前位置:网站首页>[C language] Pointer written test questions
[C language] Pointer written test questions
2022-07-04 14:34:00 【Ordinary person 1】
author :@ Ordinary person 1
special column :《C Language from 0 To 1》
In a word : In the past , All is prologue
explain : The past is irreparable , The future can change
A brief review , The content of our last article : It mainly introduces the written test questions of pointer and array . This article , We continue to strike while the iron is hot , This paper mainly introduces ——8 Pointer written test questions , Not much , Direct alignment
List of articles
The first question is
#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;
}
Code parsing :
#include <stdio.h>
int main()
{
int a[5] = {
1, 2, 3, 4, 5 };
int* ptr = (int*)(&a + 1);
//&a Take out the whole array ,&a+1 Both skip the entire array
printf("%d,%d", *(a + 1), *(ptr - 1));//2,5
//*(a+1) That's the second element 2,*(ptr-1) That is the first. 5 Elements
return 0;
}
The second question is
#include <stdio.h>
// It involves the memory alignment of the structure , The size of the structure is 20 Bytes
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
// 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;
}
Code parsing :
#include <stdio.h>
// It involves the memory alignment of the structure , The size of the structure is 20 Bytes
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
// 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);//0x10014
//p It's a structure , The size of the structure is 20 Bytes , about 16 In other words ,20 amount to 14
// So the result is 0x100014
printf("%p\n", (unsigned long)p + 0x1);//0x100001
//p It is forcibly converted into unsigned long type , The result is 0x100001
printf("%p\n", (unsigned int*)p + 0x1);//0x100004
// Unsigned integer pointer +1 Skip an integer variable
// amount to +4
return 0;
}
We might as well try the results of the trial run : The depth is reduced to the title
Third question
#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;
}
Code parsing :
#include <stdio.h>
int main()
{
int a[4] = {
1, 2, 3, 4 };
// Suppose it is stored in a small end
//01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00
int* ptr1 = (int*)(&a + 1);
//&a+1 It's equivalent to skipping the entire array , Arrived at the 4 The position after , Then force it into int*
//ptr1 Also point there
int* ptr2 = (int*)((int)a + 1);
// there a Be forcibly transformed into int type , integer +1 Namely +1, There's a difference 1 Just 1 Bytes
// Then it was forced to int*, therefore ptr2 Point to the second byte position of the first element , One byte from the first element
//ptr2 It's an integer pointer
printf("%x,%x", ptr1[-1], *ptr2);//4,2000
//ptar[-1] It can be understood as *(ptr1+(-1)), also ptr1 It's an integer pointer 4 Bytes ,-1 Jump to the 0x 00 00 00 04
// Yes ptr2 Dereference and access backwards 4 Bytes , Again with %x Print , So it is 0x 02 00 00 00
return 0;
}
Easy to understand , Draw a picture :
Some may not believe it , So let's see what happens :
Fourth question
#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;
}
Code parsing :
Look closely at the array , It's about () instead of {}, This shows that this is a comma expression , So it's equivalent to putting 1,3,5
So in fact, the elements stored in a two-dimensional array are :
#include <stdio.h>
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int* p;
p = a[0];
//a[0] Represents the address of the first element , Namely 1 The address of
printf("%d", p[0]);//1
//p[0] Can be seen as *(p+0) Namely 1
return 0;
}
Run the results :
The problem itself is not difficult , Comparison pit , We need to know the comma expression , Know what elements are actually stored in a two-dimensional array , This is the key to solving problems
Fifth question
#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;
}
Code parsing :
In order to facilitate your understanding , I drew a picture :
about a[4][2]:
about p:
p To be an assignment a,a Is the address of the first element of the array name , Namely a[0], however a[0] yes 5 Element addresses , however p nevertheless 4 Element addresses . There are differences in types . We draw pictures to understand p What's the matter :
p[4][2]:( It's the yellow area )
Back to topic , The pointer - The pointer gets the number of elements , so what ? A picture solves this problem :
Let's take a look at the running results :
Sixth question
#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;
}
After the above exercise , If you really understand it thoroughly , This problem is easier to understand :
For two dimensional arrays :
1 2 3 4 5
6 7 8 9 10
For ease of understanding , Or drawing :
about ptr1 Come on :
about ptr2:
#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));//10,5
return 0;
}
Test run results :
Question seven
#include <stdio.h>
int main()
{
char *a[] = {
"work","at","alibaba"};
char**pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
This problem is easy to understand :
about a Come on :
about pa Come on :
pa++ Is to point to the next , Point to at The location of , So the printed result is at
Test and run the results :
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;
}
This question is really interesting , It's really interesting
The first three sentences of code are a little messy , It is suggested to draw a picture and analyze the process :
Code parsing :
*++cpp: for the first time ,++cpp Point to c+2,c+2 Point to point Address , second * obtain point
*–*++cpp+3:++cpp Point to c+1 The address of , Dereference found c+1,– Just put c+1 Turned into c,c Point to ENTER The address of , In dereference , obtain ENTER,+3 Namely ENTER The third position starts , obtain ER.
*cpp[-2]+3: amount to * *(cpp-2)+3. First of all, we need to know that after the first two prepositions ++ after ,cpp It's pointing to 3 Addresses of elements , Now? -2 It is equivalent to returning to the original shape , Point to cp The address of the first element , The first dereference points to c+3, In a dereference, we get FIRST,+3 Point to ST, So the result is ST
cpp[-1][-1]+1: amount to *(*(cpp-1)-1)+1: The above one did not increase or decrease by itself , So it's the address of the third element ,cpp-1 Point to the address of the second element , Dereference to get c+2,-1 obtain c+1,c+1 Namely NEW The address of , After dereferencing, we get NEW,+1 obtain EW.
thus , This is the end of the code parsing
We can test and run the results :
summary
actually , If we have a foundation of pointer knowledge , These are natural , This exercise of the above eight questions , It's the icing on the cake , be a tiger with wings added , Train and consolidate C The core knowledge of pointer , Let us have a deeper understanding of pointer .
** meanwhile , We need to know : For some problems of pointer , We should be good at drawing , Drawing is the key step to solve the problem , This is what we must have , This is the key step , Don't neglect drawing !**
边栏推荐
- Red envelope activity design in e-commerce system
- 阿里被裁员工,找工作第N天,猎头又传来噩耗...
- remount of the / superblock failed: Permission denied
- Opencv3.2 and opencv2.4 installation
- Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
- Abnormal value detection using shap value
- [MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
- Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- Gin integrated Alipay payment
猜你喜欢
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
sql优化之explain
Combined with case: the usage of the lowest API (processfunction) in Flink framework
Node mongodb installation
Data warehouse interview question preparation
Learn kernel 3: use GDB to track the kernel call chain
Docker compose public network deployment redis sentinel mode
Talk about 10 tips to ensure thread safety
数据中台概念
Excel quickly merges multiple rows of data
随机推荐
Respect others' behavior
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
Test evaluation of software testing
[cloud native] how can I compete with this database?
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
实时数据仓库
数据湖(十三):Spark与Iceberg整合DDL操作
Leetcode T49: 字母异位词分组
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
Excel quickly merges multiple rows of data
flink sql-client. SH tutorial
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
MySQL的触发器
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
Count the running time of PHP program and set the maximum running time of PHP
Test process arrangement (3)
Transplant tinyplay for imx6q development board QT system
Talk about 10 tips to ensure thread safety
LVLG 8.2 circular scrolling animation of a label
Some problems and ideas of data embedding point