当前位置:网站首页>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;
}
边栏推荐
- C language - Introduction - Foundation - syntax - data type (4)
- Latex download installation record
- In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- 2022-2028 global special starch industry research and trend analysis report
- Leetcode (Sword finger offer) - 35 Replication of complex linked list
- Flutter tips: various fancy nesting of listview and pageview
- el-table单选并隐藏全选框
- Reading notes on how to connect the network - tcp/ip connection (II)
- Write a jison parser from scratch (6/10): parse, not define syntax
猜你喜欢
HMS core helps baby bus show high-quality children's digital content to global developers
LeetCode 74. Search 2D matrix
Some points needing attention in PMP learning
C language - Introduction - Foundation - syntax - [main function, header file] (II)
ASP. Net to access directory files outside the project website
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
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
2022-2028 global elastic strain sensor industry research and trend analysis report
Clion console output Chinese garbled code
随机推荐
After unplugging the network cable, does the original TCP connection still exist?
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
Explanation of for loop in golang
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
Sort out the power node, Mr. Wang he's SSM integration steps
Flutter tips: various fancy nesting of listview and pageview
Mac platform forgets the root password of MySQL
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
"How to connect the network" reading notes - Web server request and response (4)
The old-fashioned synchronized lock optimization will make it clear to you at once!
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
Launpad | 基础知识
Golang defer
Pueue data migration from '0.4.0' to '0.5.0' versions
ASP. Net to access directory files outside the project website
QTreeView+自定义Model实现示例
Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
Rules for using init in golang
Explanation of closures in golang