当前位置:网站首页>[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 !**
边栏推荐
- MySQL的触发器
- leetcode:6109. Number of people who know the secret [definition of DP]
- sql优化之查询优化器
- 【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
- No servers available for service: xxxx
- C # WPF realizes the real-time screen capture function of screen capture box
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
- 2022 game going to sea practical release strategy
- STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
猜你喜欢
Incremental ternary subsequence [greedy training]
Data warehouse interview question preparation
数据湖(十三):Spark与Iceberg整合DDL操作
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
LVGL 8.2 Line
leetcode:6109. Number of people who know the secret [definition of DP]
nowcoder重排链表
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
开发中常见问题总结
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
随机推荐
Oppo find N2 product form first exposure: supplement all short boards
sql优化之explain
Sqlserver functions, creation and use of stored procedures
Visual Studio调试方式详解
Test process arrangement (2)
Leetcode t49: grouping of alphabetic words
An overview of 2D human posture estimation
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
No servers available for service: xxxx
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
LVGL 8.2 LED
92. (cesium chapter) cesium building layering
Leetcode T48: rotating images
统计php程序运行时间及设置PHP最长运行时间
remount of the / superblock failed: Permission denied
【云原生】我怎么会和这个数据库杠上了?
MySQL的存储过程练习题