当前位置:网站首页>【C语言进阶篇】指针的8道笔试题
【C语言进阶篇】指针的8道笔试题
2022-07-06 17:38:00 【沐曦希】
大家好好我是沐曦希
指针的笔试题
数组名的意义:
- sizeof(数组名),这里的数组名表示整个数组,计算的是整个数组的大小。
- &数组名,这里的数组名表示整个数组,取出的是整个数组的地址。
- 除此之外所有的数组名都表示首元素的地址。
笔试题一
#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;
}
1.&a取出的是整个数组的地址,&a+1是跳过一个类型为int(*)[5]的数组。
2.a不是单独放在sizeof内,并且数组名a前面没有取地址符号,此时a表示首元素的地址。a+1时跳过一个类型为int的整型,即为第二位元素地址。
3.ptr-1是跳过一个类型为int的整型。
4.*(a+1)–>a[1];*(ptr-1)–>ptr[-1]。
笔试题二
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p = (struct Test*)0x100000;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
//已知,结构体Test类型的变量大小是20个字节
//86环境下
int main()
{
printf("%p\n", p + 0x1);//0x100000+20-->0x100014
//p+1表示跳过一个结构体,一个结构体的大小为20个字节
printf("%p\n", (unsigned long)p + 0x1);
//0x100000+1-->0x100001
printf("%p\n", (unsigned int*)p + 0x1);//0x100000+4-->0x100004
p+1表示跳过一个类型为unsigned int的整型,即四个字节
return 0;
}
笔试题三
#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;
}
1.ptr[-1]相当于*(ptr-1)
2.&a取出的是整个数组的地址,&a+1是跳过一个类型为int(*)[4]的数组。
3.a不是单独放在sizeof内,并且数组名a前面没有取地址符号,此时a表示首元素的地址。a进行了强制类型转化成int,a+1进行整型之间相加,再进行强制转化成指针,跳过了一个字节,一个int类型大小为4个字节,而数据进行的是小端存储,故由指向01的指针,指向了00。而prt2的访问权限是访问4个字节,故ptr2访问的是0x02000000。
笔试题四
#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;
}
因为int a[3][2] = {(0,1),(2,3),(4,5)}是逗号表达式。
逗号表达式,就是用逗号隔开的多个表达式。
逗号表达式,从左向右依次执行。整个表达式的结果是最后一个表达式的结果。
故int a[3][2] ={1,3,5};
a[0]是第一行的数组名,a[0]表示首元素的地址,即a[0][0]的地址,&a[0][0]。
笔试题五
#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;
}
1.p[4][2]相当于*(*(p+4)+2)。
2.两指针相减,得到的是两个指针之间的元素个数。
3.%p直接打印的是补码,地址被认为是无符号整型。
笔试题六
#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;
}
1.*(aa+1)相当于aa[1],表示第二行的数组名。
2.&aa取出的是整个数组的地址,&aa+1是跳过一个类型为int(*)[2][5]的数组。
3.aa不是单独放在sizeof内,并且数组名aa前面没有取地址符号,此时aa表示第一行的数组名,aa+1表示第二行的数组名。
笔试题七
#include <stdio.h>
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
pa++是跳过一个类型为char*的指针。
笔试题八
#include<stdio.h>
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;
}
1.*cpp[-2]相当于*(*(cpp-2))。
2.cpp[-1][-1]相当于*(*(cpp-1)-1)
printf(“%s\n”, **++cpp);结果是:
printf(“%s\n”, *-- * ++cpp + 3);结果是:
printf(“%s\n”, *cpp[-2] + 3);结果是:
printf(“%s\n”, cpp[-1][-1] + 1);结果是:
写在最后
那么指针笔试题的笔记到这里就结束了,有什么疑惑或者感觉不对的地方,可以在评论区留言。
但行前路,不负韶华!
边栏推荐
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
- Taro 小程序开启wxml代码压缩
- How to manage distributed teams?
- Body mass index program, entry to write dead applet project
- [HFCTF2020]BabyUpload session解析引擎
- Windows installation mysql8 (5 minutes)
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- Neon Optimization: an optimization case of log10 function
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- HMM 笔记
猜你喜欢
Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
免费白嫖的图床对比
go-zero微服务实战系列(九、极致优化秒杀性能)
Do you understand this patch of the interface control devaxpress WinForms skin editor?
云呐-工单管理制度及流程,工单管理规范
Installation of gazebo & connection with ROS
第三方跳转网站 出现 405 Method Not Allowed
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
boot - prometheus-push gateway 使用
随机推荐
Meet in the middle
Oracle:CDB限制PDB资源实战
Spark TPCDS Data Gen
从零开始匹配vim(0)——vimscript 简介
Using the entry level of DVA in taro3.*
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
动态规划思想《从入门到放弃》
Supersocket 1.6 creates a simple socket server with message length in the header
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
交叉验证如何防止过拟合
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
C# 计算农历日期方法 2022
golang中的atomic,以及CAS操作
How to manage distributed teams?
Part V: STM32 system timer and general timer programming
Neon Optimization: an instruction optimization case of matrix transpose
力扣1037. 有效的回旋镖
There is an error in the paddehub application
Send template message via wechat official account
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value