当前位置:网站首页>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;
} 



边栏推荐
- Write a jison parser from scratch (1/10):jison, not JSON
- 《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
- 什么是uid?什么是Auth?什么是验证器?
- Problems encountered by scan, scanf and scanln in golang
- Opencv environment construction (I)
- 《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
- 2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
- The child container margin top acts on the parent container
- Implementation principle of redis string and sorted set
- Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Logstack configuration details -- elasticstack (elk) work notes 020

回复评论的sql

Sort out the power node, Mr. Wang he's SSM integration steps

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

2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report

AMLOGIC gsensor debugging

2022-2028 global intelligent interactive tablet industry research and trend analysis report

Dede plug-in (multi-function integration)

QTreeView+自定义Model实现示例

Svg image quoted from CodeChina
随机推荐
什么是uid?什么是Auth?什么是验证器?
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
Review of last week's hot spots (6.27-7.3)
回复评论的sql
How to ensure the uniqueness of ID in distributed environment
Explanation of closures in golang
Solve the problem of "Chinese garbled MySQL fields"
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
Are there any principal guaranteed financial products in 2022?
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
What is permission? What is a role? What are users?
AMLOGIC gsensor debugging
自动化的优点有哪些?
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
In the case of easyUI DataGrid paging, click the small triangle icon in the header to reorder all the data in the database
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
2022-2028 global special starch industry research and trend analysis report
Launpad | Basics
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027