当前位置:网站首页>让学指针变得更简单(三)
让学指针变得更简单(三)
2022-07-06 07:50:00 【南航科院小张】
前言
指针面试题,对指针的理解不再停留在简单的知识层面上,而是可以知道面试题中指针的考察是怎样的;
一、指针和数组笔试题解析
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]) );
2. 指针笔试题
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;
}
总结
指针的博客就告一段落了,敬请期待后面的博客吧!!!
边栏推荐
- Sanzi chess (C language)
- Launch APS system to break the problem of decoupling material procurement plan from production practice
- The difference between TS Gymnastics (cross operation) and interface inheritance
- Binary tree creation & traversal
- HTTP cache, forced cache, negotiated cache
- National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
- WebRTC系列-H.264预估码率计算
- "Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
- 继电反馈PID控制器参数自整定
- Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
猜你喜欢
JMeter performance test steps practical tutorial
Google may return to the Chinese market after the Spring Festival.
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
It's hard to find a job when the industry is in recession
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
[count] [combined number] value series
Pangolin Library: control panel, control components, shortcut key settings
解决方案:智慧工地智能巡檢方案視頻監控系統
Key value judgment in the cycle of TS type gymnastics, as keyword use
[computer skills]
随机推荐
Scala语言学习-08-抽象类
Data governance: data quality
[count] [combined number] value series
2.10transfrom attribute
P3047 [usaco12feb]nearby cows g (tree DP)
Go learning notes (3) basic types and statements (2)
TS 类型体操 之 循环中的键值判断,as 关键字使用
1015 reversible primes (20 points) prime d-ary
23. Update data
数据治理:数据质量篇
MES, APS and ERP are essential to realize fine production
A Closer Look at How Fine-tuning Changes BERT
Games101 Lesson 7 shading 1 Notes
TS类型体操 之 字符串的妙用
MEX有关的学习
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
07- [istio] istio destinationrule (purpose rule)
Simulation of Teman green interferometer based on MATLAB
Significance and measures of encryption protection for intelligent terminal equipment
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB