当前位置:网站首页>【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专业级讲解:接口测试常用工具及测试方法,福利文
- THREE.AxesHelper is not a constructor
- Batch obtain the latitude coordinates of all administrative regions in China (to the county level)
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 系统休眠文件可以删除吗 系统休眠文件怎么删除
- [100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
- Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
- Build your own website (17)
- Rainstorm effect in levels - ue5
- Dynamic planning idea "from getting started to giving up"
猜你喜欢

Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
![[牛客] B-完全平方数](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[牛客] B-完全平方数

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

Analysis of mutex principle in golang

HMM notes

让我们,从头到尾,通透网络I/O模型

微信公众号发送模板消息

go-zero微服务实战系列(九、极致优化秒杀性能)

Provincial and urban level three coordinate boundary data CSV to JSON

JTAG debugging experience of arm bare board debugging
随机推荐
736. Lisp 语法解析 : DFS 模拟题
Provincial and urban level three coordinate boundary data CSV to JSON
Tensorflow GPU installation
How to manage distributed teams?
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
[case sharing] basic function configuration of network loop detection
【JVM调优实战100例】05——方法区调优实战(下)
Oracle: Practice of CDB restricting PDB resources
Send template message via wechat official account
【JVM调优实战100例】04——方法区调优实战(上)
力扣1037. 有效的回旋镖
Neon Optimization: summary of performance optimization experience
In rails, when the resource creation operation fails and render: new is called, why must the URL be changed to the index URL of the resource?
Force buckle 1037 Effective boomerang
身体质量指数程序,入门写死的小程序项目
ARM裸板调试之JTAG原理
What are the differences between Oracle Linux and CentOS?
from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc
动态规划思想《从入门到放弃》
NEON优化:关于交叉存取与反向交叉存取