当前位置:网站首页>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;
}
边栏推荐
- PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
- Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
- Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
- mmclassification 标注文件生成
- How web pages interact with applets
- 2022-2028 global intelligent interactive tablet industry research and trend analysis report
- 2022-2028 global protein confectionery industry research and trend analysis report
- Multilingual Wikipedia website source code development part II
- Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
- 2022-2028 global small batch batch batch furnace industry research and trend analysis report
猜你喜欢
How does idea withdraw code from remote push
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
AMLOGIC gsensor debugging
MATLAB小技巧(25)竞争神经网络与SOM神经网络
Log cannot be recorded after log4net is deployed to the server
2022-2028 global intelligent interactive tablet industry research and trend analysis report
Four common methods of copying object attributes (summarize the highest efficiency)
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
How do microservices aggregate API documents? This wave of show~
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
随机推荐
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
About the for range traversal operation in channel in golang
How web pages interact with applets
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Pueue data migration from '0.4.0' to '0.5.0' versions
回复评论的sql
How should PMP learning ideas be realized?
技术管理进阶——如何设计并跟进不同层级同学的绩效
品牌连锁店5G/4G无线组网方案
Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
lolcat
UML sequence diagram [easy to understand]
What is uid? What is auth? What is a verifier?
Four common methods of copying object attributes (summarize the highest efficiency)
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
How to write unit test cases
PMP registration process and precautions
IIS configure FTP website
Lauchpad X | 模式