当前位置:网站首页>C language pointer interview question - the second bullet
C language pointer interview question - the second bullet
2022-07-04 09:38:00 【Hair is not as much as code】
Catalog
The first question is
#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 The type is int(*)[5], there (int*) It is mandatory to cast &a Type conversion to int*
ptr-1 Then it will point to 5, Then dereference , final result *(ptr-1) be equal to 5
a Point to first element ,a+1 Point to the second element
The second question is
#include<stdio.h>
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p=(struct Test*)0x100000;
// hypothesis p The value of is 0x100000. What are the values of the expressions in the following table ?
// It is known that , Structure Test The variable size of type is 20 Bytes
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
because Test The size of the type is 20 Bytes , and p Is precisely test type ,0x1 yes 16 Number in hexadecimal 1, yes 1*16^0,
p+0x1: It is equivalent to giving p Add twenty bytes , and p The content is 0x1000000, This is a 16 Number in hexadecimal , We should put this 20 Convert to 16 Number in hexadecimal , The result of the conversion is 14, So the answer is 0x100000+14=0x100014
(unsigned long)p+0x1: Is to put p Cast to integer ( Unsigned long shaping ), After plastic surgery, the result is 1048576, Then add 1(16 It's binary 1 and 10 It's binary 1 identical ), Turn into 1048577, To 16 Into the system for 0x100001
(unsigned int*)p: hold p Cast to (unsigned int *) type , The size of this type of permission is four bytes , When p+0x1=p+1 after , Because the permission size is 4 Bytes , therefore p Across 4 Bytes, so the result is 0x100004
Third question
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 The original type of is int(*)[4], Cast type to (int*)ptr[-1] How to get , Please look at the picture ,ptr[-1]=*(ptr1+(-1))=*(ptr1-1)
*ptr2, The end result is because *ptr It is an integer dereference , So the next four bytes are accessed , The final result is related to small end storage
Fourth question
#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;
}
Fifth question
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;
}
Here is the -4 Print it as an address
Sixth question
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;
}
Question seven
#include <stdio.h>
int main()
{
char *a[] = {"work","at","alibaba"};
char**pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
because pa The object is char * Of , So every time 1, Add one more char * Type size
The eighth question
#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;
}
边栏推荐
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- About the for range traversal operation in channel in golang
- 《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
- Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
- Some points needing attention in PMP learning
- 什么是uid?什么是Auth?什么是验证器?
- MySQL foundation 02 - installing MySQL in non docker version
- Golang 类型比较
- Sort out the power node, Mr. Wang he's SSM integration steps
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
C语言指针面试题——第二弹
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
Four common methods of copying object attributes (summarize the highest efficiency)
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
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
How to batch change file extensions in win10
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
How should PMP learning ideas be realized?
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
2022-2028 global optical transparency industry research and trend analysis report
随机推荐
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
Explanation of closures in golang
Go context 基本介绍
2022-2028 global optical transparency industry research and trend analysis report
IIS configure FTP website
什么是uid?什么是Auth?什么是验证器?
Markdown syntax
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
Golang Modules
Are there any principal guaranteed financial products in 2022?
mmclassification 标注文件生成
C语言指针经典面试题——第一弹
ArrayBuffer
Deadlock in channel
2022-2028 global probiotics industry research and trend analysis report
Kubernetes CNI 插件之Fabric
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028