当前位置:网站首页>【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);结果是:
写在最后
那么指针笔试题的笔记到这里就结束了,有什么疑惑或者感觉不对的地方,可以在评论区留言。
但行前路,不负韶华!
边栏推荐
- [Niuke] b-complete square
- 分享一个通用的so动态库的编译方法
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
- THREE.AxesHelper is not a constructor
- LLDP兼容CDP功能配置
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Installation of gazebo & connection with ROS
- Part V: STM32 system timer and general timer programming
- JTAG debugging experience of arm bare board debugging
- 【案例分享】网络环路检测基本功能配置
猜你喜欢
【JVM调优实战100例】04——方法区调优实战(上)
Installation of gazebo & connection with ROS
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Force buckle 1037 Effective boomerang
Body mass index program, entry to write dead applet project
身体质量指数程序,入门写死的小程序项目
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
C language - array
随机推荐
BFS realizes breadth first traversal of adjacency matrix (with examples)
Pytorch中torch和torchvision的安装
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
THREE.AxesHelper is not a constructor
Grc: personal information protection law, personal privacy, corporate risk compliance governance
Fastdfs data migration operation record
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Provincial and urban level three coordinate boundary data CSV to JSON
身体质量指数程序,入门写死的小程序项目
How to evaluate load balancing performance parameters?
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Make a simple graphical interface with Tkinter
Part V: STM32 system timer and general timer programming
交叉验证如何防止过拟合
NEON优化:矩阵转置的指令优化案例
树莓派/arm设备上安装火狐Firefox浏览器
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
Build your own website (17)