当前位置:网站首页>[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 !**
边栏推荐
- 数据湖(十三):Spark与Iceberg整合DDL操作
- MySQL的存储过程练习题
- Digi restarts XBee Pro S2C production. Some differences need to be noted
- Some problems and ideas of data embedding point
- 【信息检索】链接分析
- 聊聊保证线程安全的 10 个小技巧
- 失败率高达80%,企业数字化转型路上有哪些挑战?
- Leetcode T47: 全排列II
- LVGL 8.2 Sorting a List using up and down buttons
- Programmer turns direction
猜你喜欢
Chapter 17 process memory
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
Use of tiledlayout function in MATLAB
实时数据仓库
LVGL 8.2 text shadow
WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能
Digi重启XBee-Pro S2C生产,有些差别需要注意
Data warehouse interview question preparation
随机推荐
第十七章 进程内存
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
First experience of ViewModel
LiveData
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
leetcode:6109. Number of people who know the secret [definition of DP]
聊聊保证线程安全的 10 个小技巧
关于miui12.5 红米k20pro用au或者povo2出现问题的解决办法
C language book rental management system
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
【信息检索】链接分析
LVGL 8.2 Draw label with gradient color
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Red envelope activity design in e-commerce system
Industrial Internet has greater development potential and more industry scenarios
使用CLion编译OGLPG-9th-Edition源码
Ultrasonic distance meter based on 51 single chip microcomputer
Progress in architecture
Oppo find N2 product form first exposure: supplement all short boards
MySQL的存储过程练习题