当前位置:网站首页>【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);结果是:

写在最后

那么指针笔试题的笔记到这里就结束了,有什么疑惑或者感觉不对的地方,可以在评论区留言。
但行前路,不负韶华!
边栏推荐
- 资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
- golang中的atomic,以及CAS操作
- There is an error in the paddehub application
- How to evaluate load balancing performance parameters?
- [牛客] [NOIP2015]跳石头
- NEON优化:log10函数的优化案例
- 405 method not allowed appears when the third party jumps to the website
- Neon Optimization: performance optimization FAQ QA
- taro3.*中使用 dva 入门级别的哦
- The difference between spin and sleep
猜你喜欢
![[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](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[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

【案例分享】网络环路检测基本功能配置

Send template message via wechat official account

UI control telerik UI for WinForms new theme - vs2022 heuristic theme

Niuke cold training camp 6B (Freund has no green name level)

HMM 笔记

BFS realizes breadth first traversal of adjacency matrix (with examples)

界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?

Body mass index program, entry to write dead applet project

Analysis of mutex principle in golang
随机推荐
Meet in the middle
云呐-工单管理制度及流程,工单管理规范
Analysis of mutex principle in golang
Body mass index program, entry to write dead applet project
table表格设置圆角
[Niuke] [noip2015] jumping stone
Segmenttree
ARM裸板调试之JTAG调试体验
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
Wood extraction in Halcon
C# 计算农历日期方法 2022
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
What are the differences between Oracle Linux and CentOS?
BFS realizes breadth first traversal of adjacency matrix (with examples)
How to manage distributed teams?
Neon Optimization: an instruction optimization case of matrix transpose
[HFCTF2020]BabyUpload session解析引擎
Boot - Prometheus push gateway use
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
The cost of returning tables in MySQL