当前位置:网站首页>C语言指针面试题——第二弹
C语言指针面试题——第二弹
2022-07-04 09:32:00 【头发没有代码多】
目录
第一题
#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;
}



&a的类型是int(*)[5],这里的(int*)是强制类型转换把&a类型转换为int*
ptr-1之后会指到5,然后再解引用,最终结果*(ptr-1)等于5
a指向首元素,a+1指向第二个元素
第二题
#include<stdio.h>
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p=(struct Test*)0x100000;
//假设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;
}

由于Test类型的大小是20个字节,而p正好是test类型 ,0x1是16进制下的数字1,是1*16^0,
p+0x1:相当于给p加了二十个字节,而p的内容是0x1000000,这是16进制下的数字,我们应把这20转换为16进制下的数字,转换结果为14,所以答案是0x100000+14=0x100014
(unsigned long)p+0x1:就是把p强制转换为整形(无符号长整形),转为整形后结果是1048576,之后再加1(16进制的1和10进制的1相同),变为1048577,转为16进制为0x100001
(unsigned int*)p:把p强制转换为(unsigned int *)类型,这个类型的权限大小是四个字节,当p+0x1=p+1之后,由于权限大小为4个字节,所以p跨过4个字节因此结果为0x100004
第三题
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;
} 
ptr1的类型原来是int(*)[4],强制类型转换为(int*)ptr[-1]如何得来,请看上图,ptr[-1]=*(ptr1+(-1))=*(ptr1-1)
*ptr2,最终结果是因为*ptr是整形解引用,所以访问了后面的四个字节,最终结果和小端存储有关

第四题
#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;
} 
这里把-4当作地址去打印了
第六题
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;
} 
因为pa指向的对象是char *的,所以每次加1,加一个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;
} 



边栏推荐
- Daughter love: frequency spectrum analysis of a piece of music
- Pueue data migration from '0.4.0' to '0.5.0' versions
- Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
- Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
- Reading notes on how to connect the network - tcp/ip connection (II)
- 2022-2028 global strain gauge pressure sensor industry research and trend analysis report
- Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
- 2022-2028 global elastic strain sensor industry research and trend analysis report
- MySQL foundation 02 - installing MySQL in non docker version
猜你喜欢
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)

Svg image quoted from CodeChina

Target detection -- intensive reading of yolov3 paper

Four common methods of copying object attributes (summarize the highest efficiency)

2022-2028 global strain gauge pressure sensor industry research and trend analysis report

If you can quickly generate a dictionary from two lists

Solve the problem of "Chinese garbled MySQL fields"

Markdown syntax

【LeetCode 42】501. Mode in binary search tree

2022-2028 global intelligent interactive tablet industry research and trend analysis report
随机推荐
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
Reading notes of how the network is connected - understanding the basic concepts of the network (I)
C language - Introduction - Foundation - syntax - data type (4)
Go context 基本介绍
If you can quickly generate a dictionary from two lists
Launpad | 基礎知識
UML sequence diagram [easy to understand]
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
ArrayBuffer
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
mmclassification 标注文件生成
What is uid? What is auth? What is a verifier?
Tkinter Huarong Road 4x4 tutorial II
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
How should PMP learning ideas be realized?
el-table单选并隐藏全选框
Svg image quoted from CodeChina
Leetcode (Sword finger offer) - 35 Replication of complex linked list
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Fatal error in golang: concurrent map writes