当前位置:网站首页>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;
}
边栏推荐
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
- 2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
- Latex download installation record
- ASP. Net to access directory files outside the project website
- Service call feign of "micro service"
- MySQL foundation 02 - installing MySQL in non docker version
- Problems encountered by scan, scanf and scanln in golang
- After unplugging the network cable, does the original TCP connection still exist?
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
猜你喜欢
Logstack configuration details -- elasticstack (elk) work notes 020
Dede plug-in (multi-function integration)
The child container margin top acts on the parent container
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
Web端自动化测试失败原因汇总
Ultimate bug finding method - two points
ASP. Net to access directory files outside the project website
GoLand environment variable configuration
How to ensure the uniqueness of ID in distributed environment
Log cannot be recorded after log4net is deployed to the server
随机推荐
AMLOGIC gsensor debugging
Nurse level JDEC addition, deletion, modification and inspection exercise
lolcat
You can see the employment prospects of PMP project management
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
About the for range traversal operation in channel in golang
Solution to null JSON after serialization in golang
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
What is permission? What is a role? What are users?
Clion console output Chinese garbled code
Implementing expired localstorage cache with lazy deletion and scheduled deletion
2022-2028 global tensile strain sensor industry research and trend analysis report
C language - Introduction - Foundation - syntax - [main function, header file] (II)
Explanation of for loop in golang
Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
如何编写单元测试用例
什么是权限?什么是角色?什么是用户?
The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
Logstack configuration details -- elasticstack (elk) work notes 020
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)