当前位置:网站首页>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;
}
边栏推荐
- 【LeetCode 42】501. Mode in binary search tree
- Write a jison parser from scratch (6/10): parse, not define syntax
- How to batch change file extensions in win10
- lolcat
- Daughter love: frequency spectrum analysis of a piece of music
- 165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
- PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun
- Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
- 2022-2028 global special starch industry research and trend analysis report
- H5 audio tag custom style modification and adding playback control events
猜你喜欢
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
Daughter love: frequency spectrum analysis of a piece of music
Mantis creates users without password options
2022-2028 global special starch industry research and trend analysis report
2022-2028 global seeder industry research and trend analysis report
You can see the employment prospects of PMP project management
How to batch change file extensions in win10
LeetCode 74. Search 2D matrix
Ultimate bug finding method - two points
随机推荐
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
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
Launpad | basic knowledge
2022-2028 global protein confectionery industry research and trend analysis report
MySQL foundation 02 - installing MySQL in non docker version
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
How to ensure the uniqueness of ID in distributed environment
《网络是怎么样连接的》读书笔记 - FTTH
Service call feign of "micro service"
【LeetCode 42】501. Mode in binary search tree
Reading notes on how to connect the network - tcp/ip connection (II)
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
C language - Introduction - Foundation - syntax - data type (4)
Sort out the power node, Mr. Wang he's SSM integration steps
Lauchpad x | MODE
2022-2028 global small batch batch batch furnace industry research and trend analysis report
UML 时序图[通俗易懂]