当前位置:网站首页>c语言指针运算
c语言指针运算
2022-08-02 05:19:00 【Code Writers】
文章目录
前言
一、指针和数组笔试题解析
1.1 一维数组
1.2 字符数组
1.3 二维数组
2. 指针笔试题
总结
前言
指针面试题,对指针的理解不再停留在简单的知识层面上,而是可以知道面试题中指针的考察是怎样的;
一、指针和数组笔试题解析
1.1 一维数组
首先说一下知识点:很重要!!!
数组名的意义:
sizeof(数组名),这里的数组名表示整个数组,计算的是整个数组的大小。
&数组名,这里的数组名表示整个数组,取出的是整个数组的地址。
除此之外所有的数组名都表示首元素的地址。
int a[ ] = {1,2,3,4};
printf( “%d\n”,sizeof(a) );
printf( “%d\n”,sizeof(a+0) );
printf( “%d\n”,sizeof(a) );
printf( “%d\n”,sizeof(a+1) );
printf( “%d\n”,sizeof(a[1]) );
printf( “%d\n”,sizeof(&a) );
printf( “%d\n”,sizeof(&a) ;
printf( “%d\n”,sizeof(&a+1) );
printf( “%d\n”,sizeof(&a[0]) );
printf( “%d\n”,sizeof(&a[0]+1) );
1.2 字符数组
char arr[] = {‘a’,‘b’,‘c’,‘d’,‘e’,‘f’};
printf(“%d\n”, sizeof(arr));
printf(“%d\n”, sizeof(arr+0));
printf(“%d\n”, sizeof(*arr));
printf(“%d\n”, sizeof(arr[1]));
printf(“%d\n”, sizeof(&arr));
printf(“%d\n”, sizeof(&arr+1));
printf(“%d\n”, sizeof(&arr[0]+1));
printf(“%d\n”, strlen(arr));
printf(“%d\n”, strlen(arr+0));
printf(“%d\n”, strlen(*arr));
printf(“%d\n”, strlen(arr[1]));
printf(“%d\n”, strlen(&arr));
printf(“%d\n”, strlen(&arr+1));
printf(“%d\n”, strlen(&arr[0]+1));
char arr[] = “abcdef”;
printf(“%d\n”, sizeof(arr));
printf(“%d\n”, sizeof(arr+0));
printf(“%d\n”, sizeof(*arr));
printf(“%d\n”, sizeof(arr[1]));
printf(“%d\n”, sizeof(&arr));
printf(“%d\n”, sizeof(&arr+1));
printf(“%d\n”, sizeof(&arr[0]+1));
printf(“%d\n”, strlen(arr));
printf(“%d\n”, strlen(arr+0));
printf(“%d\n”, strlen(*arr));
printf(“%d\n”, strlen(arr[1]));
printf(“%d\n”, strlen(&arr));
printf(“%d\n”, strlen(&arr+1));
printf(“%d\n”, strlen(&arr[0]+1));
char *p = “abcdef”;
printf(“%d\n”, sizeof§);
printf(“%d\n”, sizeof(p+1));
printf(“%d\n”, sizeof(*p));
printf(“%d\n”, sizeof(p[0]));
printf(“%d\n”, sizeof(&p));
printf(“%d\n”, sizeof(&p+1));
printf(“%d\n”, sizeof(&p[0]+1));
printf(“%d\n”, strlen§);
printf(“%d\n”, strlen(p+1));
printf(“%d\n”, strlen(*p));
printf(“%d\n”, strlen(p[0]));
printf(“%d\n”, strlen(&p));
printf(“%d\n”, strlen(&p+1));
printf(“%d\n”, strlen(&p[0]+1));
1.3 二维数组
int a[3][4] = {0};
printf( “%d\n”,sizeof(a) );
printf( “%d\n",sizeof(a[0][0]) );
printf( “%d\n”,sizeof(a[0]) );
printf( ”%d\n",sizeof(a[0]+1) );
printf( “%d\n”,sizeof(* (a[0]+1)) );
printf( “%d\n”,sizeof(a+1) );
printf( “%d\n”,sizeof(* ( a+1) ) );
printf( “%d\n”,sizeof(&a[0]+1) );
printf( “%d\n”,sizeof( * (&a[0]+1) ) );
printf( “%d\n”,sizeof(*a) );
printf( “%d\n”,sizeof(a[3]) );
- 指针笔试题
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( “%d,%d”, *(a + 1), *(ptr - 1));
return 0;
}
//程序的结果是什么?
//由于还没学习结构体,这里告知结构体的大小是20个字节
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
//已知,结构体Test类型的变量大小是20个字节
int main()
{
printf(“%p\n”, p + 0x1);
printf(“%p\n”, ( unsigned long )p + 0x1);
printf(“%p\n”, (unsigned int * )p + 0x1);
return 0;
}
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;
}
#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 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;
}
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;
}
#include <stdio.h>
int main()
{
char *a[] = {“work”,“at”,“alibaba”};
char**pa = a;
pa++;
printf(“%s\n”, *pa);
return 0;
}
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;
}
总结
指针的博客就告一段落了,敬请期待后面的博客吧!!!
边栏推荐
- npm does not recognize the "npm" item as the name of a cmdlet, function, script file, or runnable program.Please check the spelling of the name, and if the path is included, make sure the path is corr
- Not annotated parameter overrides @NonNullApi parameter
- 股价屡创新低 地产SaaS巨头陷入困境 明源云该如何转型自救?
- Analysis of port 9848 error at startup of Nacos client (non-version upgrade problem)
- MarkDown Formula Instruction Manual
- C语言操作符详解(2)
- zabbix自动发现和自动注册
- 6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
- MySQL database video tutorial from beginner to proficient
- Different ways of shell scripting
猜你喜欢

nacos registry

npm、cnpm的安装

The installation of NPM, CNPM

使用TinkerPop框架对GDB增删改查

Stress testing and performance analysis of node projects

npm does not recognize the "npm" item as the name of a cmdlet, function, script file, or runnable program.Please check the spelling of the name, and if the path is included, make sure the path is corr

Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue

Block elements, inline elements (
elements, span elements)
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略

Shell 脚本不同玩法
随机推荐
Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue
秒杀系统小demo
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
C语言小游戏——扫雷小游戏
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
nacos registry
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
Double for loop case (use js jiujiu printing multiplication table)
5款经典代码阅读器的使用方案对比
HCIP BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合
如何进行并发数计算(稳定性测试和压力测试)?
VMTK环境配置记录
BGP experiment (route reflector, federation, route optimization)
Point Density-Aware Voxels for LiDAR 3D Object Detection Paper Notes
Nacos注册中心的部署与用法详细介绍
The advantages of making web3d dynamic product display
npm 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
APP Bluetooth connection test of test technology
洛谷小游戏大全(用洛谷的人都得知道)
Guarantee WIFI security in home and enterprise-with AC and AP networking experiment