当前位置:网站首页>[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 !**
边栏推荐
- 【信息检索】链接分析
- Leetcode t47: full arrangement II
- leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
- Digi重启XBee-Pro S2C生产,有些差别需要注意
- 【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
- An overview of 2D human posture estimation
- MySQL的存储过程练习题
- Docker compose public network deployment redis sentinel mode
- 开发中常见问题总结
- 【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
猜你喜欢
Learn kernel 3: use GDB to track the kernel call chain
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
[information retrieval] experiment of classification and clustering
Data Lake (13): spark and iceberg integrate DDL operations
实战解惑 | OpenCV中如何提取不规则ROI区域
Practical puzzle solving | how to extract irregular ROI regions in opencv
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
软件测试之测试评估
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
SqlServer函数,存储过程的创建和使用
随机推荐
Talk about 10 tips to ensure thread safety
92. (cesium chapter) cesium building layering
开发中常见问题总结
Nowcoder rearrange linked list
Combined with case: the usage of the lowest API (processfunction) in Flink framework
Ultrasonic distance meter based on 51 single chip microcomputer
利用Shap值进行异常值检测
LVGL 8.2 Sorting a List using up and down buttons
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
Gin integrated Alipay payment
[cloud native] how can I compete with this database?
leetcode:6109. Number of people who know the secret [definition of DP]
C language programming
Excel quickly merges multiple rows of data
LVGL 8.2 Draw label with gradient color
Matters needing attention in overseas game Investment Agency
聊聊保证线程安全的 10 个小技巧
Compile oglpg-9th-edition source code with clion
统计php程序运行时间及设置PHP最长运行时间
迅为IMX6Q开发板QT系统移植tinyplay