当前位置:网站首页>[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 !**
边栏推荐
- Data Lake (13): spark and iceberg integrate DDL operations
- WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能
- 使用CLion编译OGLPG-9th-Edition源码
- [MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
- LVGL 8.2 Menu
- Chapter 17 process memory
- STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
- [cloud native] how can I compete with this database?
- LVGL 8.2 LED
- sql优化之explain
猜你喜欢
Nowcoder reverse linked list
Incremental ternary subsequence [greedy training]
Oppo find N2 product form first exposure: supplement all short boards
No servers available for service: xxxx
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
Explain of SQL optimization
聊聊保证线程安全的 10 个小技巧
Detailed explanation of visual studio debugging methods
Xcode abnormal pictures cause IPA packet size problems
Leetcode T48: rotating images
随机推荐
使用CLion编译OGLPG-9th-Edition源码
Classify boost libraries by function
Chapter 16 string localization and message Dictionary (2)
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
Leetcode t47: full arrangement II
Popular framework: the use of glide
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
Nowcoder rearrange linked list
SqlServer函数,存储过程的创建和使用
实战解惑 | OpenCV中如何提取不规则ROI区域
sql优化之查询优化器
Leetcode t49: grouping of alphabetic words
失败率高达80%,企业数字化转型路上有哪些挑战?
关于miui12.5 红米k20pro用au或者povo2出现问题的解决办法
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
Leetcode 61: rotating linked list
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
架构方面的进步
LVGL 8.2 LED
No servers available for service: xxxx