当前位置:网站首页>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;
}
边栏推荐
- Logstack configuration details -- elasticstack (elk) work notes 020
- Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
- Golang Modules
- Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
- Leetcode (Sword finger offer) - 35 Replication of complex linked list
- In the case of easyUI DataGrid paging, click the small triangle icon in the header to reorder all the data in the database
- Trees and graphs (traversal)
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- IIS configure FTP website
- C # use smtpclient The sendasync method fails to send mail, and always returns canceled
猜你喜欢
How does idea withdraw code from remote push
智能网关助力提高工业数据采集和利用
mmclassification 标注文件生成
Some points needing attention in PMP learning
C语言指针经典面试题——第一弹
Ultimate bug finding method - two points
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
品牌连锁店5G/4G无线组网方案
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
H5 audio tag custom style modification and adding playback control events
随机推荐
Golang 类型比较
The child container margin top acts on the parent container
You can see the employment prospects of PMP project management
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
Opencv environment construction (I)
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
品牌连锁店5G/4G无线组网方案
自动化的优点有哪些?
Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
Pueue data migration from '0.4.0' to '0.5.0' versions
Hands on deep learning (34) -- sequence model
Logstack configuration details -- elasticstack (elk) work notes 020
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
MySQL foundation 02 - installing MySQL in non docker version
什么是权限?什么是角色?什么是用户?