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

写在最后

那么指针笔试题的笔记到这里就结束了,有什么疑惑或者感觉不对的地方,可以在评论区留言。
但行前路,不负韶华!
边栏推荐
- UI控件Telerik UI for WinForms新主题——VS2022启发式主题
- Oracle: Practice of CDB restricting PDB resources
- 2022 Google CTF SEGFAULT LABYRINTH wp
- BFS realizes breadth first traversal of adjacency matrix (with examples)
- Using the entry level of DVA in taro3.*
- Receive user input, height BMI, BMI detection small business entry case
- Docker method to install MySQL
- There is an error in the paddehub application
- 2022 Google CTF SEGFAULT LABYRINTH wp
- 接收用户输入,身高BMI体重指数检测小业务入门案例
猜你喜欢

Can the system hibernation file be deleted? How to delete the system hibernation file

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
![[牛客] B-完全平方数](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[牛客] B-完全平方数

Batch obtain the latitude coordinates of all administrative regions in China (to the county level)

如何管理分布式团队?

Tensorflow GPU installation

移植DAC芯片MCP4725驱动到NUC980

动态规划思想《从入门到放弃》

Go zero micro service practical series (IX. ultimate optimization of seckill performance)
![[HFCTF2020]BabyUpload session解析引擎](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[HFCTF2020]BabyUpload session解析引擎
随机推荐
Do you understand this patch of the interface control devaxpress WinForms skin editor?
Atomic in golang, and cas Operations
接收用户输入,身高BMI体重指数检测小业务入门案例
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
NEON优化:性能优化常见问题QA
Supersocket 1.6 creates a simple socket server with message length in the header
The cost of returning tables in MySQL
C language - array
斗地主游戏的案例开发
How to manage distributed teams?
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Segmenttree
Meet in the middle
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
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
Fastdfs data migration operation record
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
Rainstorm effect in levels - ue5