当前位置:网站首页>指针运算相关面试题详解【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到了,再结合图解,就很简单了

结语:写得很水,哈哈哈哈,但是大概意思就表达出来了,写在这里主要是方便自己以后的复习和学习。
边栏推荐
- "A minute" Copy siege lion log 】 【 run MindSpore LeNet model
- bind()系统调用的用处
- MNIST Handwritten Digit Recognition - Image Analysis Method for Binary Classification
- Golang环境变量设置(二)--GOMODULE&GOPROXY
- No matching function for call to ‘RCTBridgeModuleNameForClass‘
- IEEE802.X protocol suite
- 基于asp.net的法律援助平台的设计与实现(附项目链接)
- [开发杂项][编辑器][代码阅读]ctags&vim
- Completely remove MySQL tutorial
- C语言无符号整型运算
猜你喜欢

Cut the hit pro subtitles export of essays

Golang environment variable settings (2)--GOMODULE & GOPROXY

Deep learning, "grain and grass" first--On the way to obtain data sets

Install Minikube Cluster in AWS-EC2

集合---ArrayList的底层

tmux概念和使用

IDEA创建Servlet步骤

Brief description of database and common operation guide

结构体内存对齐-C语言

MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程
随机推荐
位段-C语言
卷积神经网络入门详解
【Copy攻城狮日志】“一分钟”跑通MindSpore的LeNet模型
Install Minikube Cluster in AWS-EC2
CAS无锁队列的实现
Copy Siege Lion's Annual "Battle" | Review 2020
[开发杂项][VS Code]remote-ssd retry failed
文件编辑器
C语言结构体(必须掌握版)
[Daily office][shell] Common code snippets
MNIST handwritten digit recognition - based on Mindspore to quickly build a perceptron to achieve ten categories
No matching function for call to ‘RCTBridgeModuleNameForClass‘
[Deep Learning Diary] Day 1: Hello world, Hello CNN MNIST
DRA821 环境搭建
arm-3-中断体系结构
详解近端策略优化
bind()系统调用的用处
LeetCode_Dec_3rd_Week
MNIST手写数字识别 —— ResNet-经典卷积神经网络
MNIST handwritten digit recognition, sorted by from two to ten