当前位置:网站首页>指针运算相关面试题详解【C语言】
指针运算相关面试题详解【C语言】
2022-08-04 05:31:00 【crazy__xieyi】
接着上一篇文章,咱们在理解了数组名的含义,以及指针的运算之后,下面咱们来关注一些经典的指针面试题。下面直接代码+图解。
第一题:
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int* ptr = (int*)(&a + 1);
printf("%d,%d", *(a + 1), *(ptr - 1));// 2 5
return 0;
}
第二题:
假设p 的值为0x100000。 如下表表达式的值分别为多少?
已知,结构体Test类型的变量大小是20个字节
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}* p = (struct Test*)0x100000;
int main()
{
printf("%p\n", p + 0x1);
//0x100000+20-->0x100014
printf("%p\n", (unsigned long)p + 0x1);
//1,048,576+1 --> 1,048,577
//0x100001
printf("%p\n", (unsigned int*)p + 0x1);
//0x100000+4-->0x100004
return 0;
}第三题:
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;
}
注意:这里需要清楚小端存储的概念,具体内容想看前面有一篇文章的内容,里面详细讲解了大小端存储。
当要求十六进制打印时,结果为:

第四题:
int main()
{
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
int* p;
p = a[0];
printf("%d", p[0]);
return 0;
}注意:这里需要注意括号问题!还有就是逗号表达式的问题!!!

第五题:
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;
}注意:这里首先需要理解:p[4][2] -> *(*(p+4)+2),而且这两个指针相减为中间的元素个数


第六题:
int main()
{
char* c[] = { "ENTER","NEW","POINT","FIRST" };
char** cp[] = { c + 3,c + 2,c + 1,c };
char*** cpp = cp;
printf("%s\n", **++cpp);// POINT
printf("%s\n", *-- * ++cpp + 3); // ER
printf("%s\n", *cpp[-2] + 3); // ST
printf("%s\n", cpp[-1][-1] + 1); // EW
return 0;
}注意:这道题要注意-- 、++ 操作会改变cpp的位置,如果这一点get到了,再结合图解,就很简单了

结语:写得很水,哈哈哈哈,但是大概意思就表达出来了,写在这里主要是方便自己以后的复习和学习。
边栏推荐
- makefile基础学习
- "A minute" Copy siege lion log 】 【 run MindSpore LeNet model
- 安装MySQL的详细步骤
- An abstract class, internal classes and interfaces
- The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
- 中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
- arm learning-1-development board
- LeetCode_22_Apr_2nd_Week
- (导航页)OpenStack-M版-双节点手工搭建-附B站视频
- 腾讯、网易纷纷出手,火到出圈的元宇宙到底是个啥?
猜你喜欢

No matching function for call to ‘RCTBridgeModuleNameForClass‘

Shell脚本执行的三种方式

迅雷关闭自动更新

LeetCode_Nov_4th_Week

中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能

腾讯、网易纷纷出手,火到出圈的元宇宙到底是个啥?

target has libraries with conflicting names: libcrypto.a and libssl.a.

在AWS-EC2中安装Minikube集群

Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers

Copy攻城狮5分钟在线体验 MindIR 格式模型生成
随机推荐
Pytest common plug-in
[日常办公][shell]常用代码段
第三章 标准单元库(上)
数据库的简述与常用操作指南
管道重定向
Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
Deep Learning Theory - Initialization, Parameter Adjustment
AWS uses EC2 to reduce the training cost of DeepRacer: DeepRacer-for-cloud practical operation
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
[CV-Learning] Linear Classifier (SVM Basics)
LeetCode_Nov_4th_Week
Completely remove MySQL tutorial
Windows10重置MySQL用户密码
安装MySQL的详细步骤
A code example of the PCL method in the domain of DG (Domain Generalization)
剪映专业版字幕导出随笔
机器学习——分类问题对于文字标签的处理(特征工程)
位段-C语言
ideal life
Thunderbolt turns off automatic updates