当前位置:网站首页>Analysis of pointer and array written test questions
Analysis of pointer and array written test questions
2022-07-06 08:03:00 【Pick up the famous Rose】
List of articles
Preface
I haven't seen you for a long time !!! Recently, due to the school exam , I haven't updated my blog , It's not until these two days that I finish the school exam , Ha ha ha ha ha ha , I can't wait to explain it to you !!!
Today I'll tell you about the interview questions about arrays and pointers , The content may be difficult , There are knowledge points that you can refer to
Another thing about this host is 32 Bit environment , So the default size of the pointer is 4 Bytes !!!
One 、 Pointer and array written test question analysis
1. One dimensional array
int main() {
int a[] = {
1,2,3,4 };
//sizeof( Array name ), The array name represents the entire array , It calculates the size of the entire array , Unit is byte
printf("%d\n", sizeof(a));//16
//a Not alone sizeof Inside , No address , therefore a Is the first element address ,a+0 Or the address of the first element
printf("%d\n", sizeof(a + 0));//4/8
//*a Medium a Is the address of the first element of the array ,*a Is to dereference the address of the first element , What you find is the first element
// The size of the first element is 4 Bytes
printf("%d\n", sizeof(*a));//4
// here a Address of the first element of the array
//a+1 Is the address of the second element
//sizeof(a+1) Is the size of the address
printf("%d\n", sizeof(a + 1));//4/8
// It calculates the size of the second element
printf("%d\n", sizeof(a[1]));//4
//&a The address of the extracted array , Address of array , It's just an address
printf("%d\n", sizeof(&a));//4/8
//&a------>int(*)[4]
// &a What you get is the address of the array name , The type is int(*)[4], Is a set of array pointers
// Array pointer dereference is equivalent to accessing an array
// &*a---->a
//2.
//* and & Offset amount to sizeof(a)
printf("%d\n", sizeof(*&a));//16
//&a--> What is taken out is the pointer of the array
//&a-->int(*)[4]
//&a+1 Is from an array a The address of skipped a backward (4 Of shaping elements ) Size of array
//&a+1 Or the address , Yes, the address is 4 Bytes
printf("%d\n", sizeof(&a + 1));//4/8
//&a[0] It's the address of the first element
// It calculates the size of the address
printf("%d\n", sizeof(&a[0]));//4/8
//&a[0]+1 Is the address of the second element
// It calculates the size of the address
//&a[0]+1---->&a[1]
printf("%d\n", sizeof(&a[0] + 1));//4/8
return 0;
}
2. A character array
int main() {
char arr[] = {
'a','b','c','d','e','f' };
//sizeof( Array name )
printf("%d\n", sizeof(arr));//6
//arr+0 Is the address of the first element of the array
printf("%d\n", sizeof(arr + 0));//4/8
//*arr Is the first element of the array , The size is one byte
//*arr--->arr[0]
//*(arr+0)---->arr[0]
printf("%d\n", sizeof(*arr));//1
//*arr Is the second element of the array , The size is one byte
printf("%d\n", sizeof(arr[1]));//1
//&arr Is the address of the array , Yes, the address is 4/8 Bytes
printf("%d\n", sizeof(&arr));//4/8
//&arr + 1 Is the address after the array
printf("%d\n", sizeof(&arr + 1));//4/8
//(&arr[0] + 1) Is the second element of the array , The size is one byte
printf("%d\n", sizeof(&arr[0] + 1));//4/8
printf("%d\n", strlen(arr));// Random value
printf("%d\n", strlen(arr + 0));// Random value
//printf("%d\n", strlen(*arr));//--->strlen('a');--->strlen(97);// Wild pointer
There is a problem
--->strlen('b');--->strlen(98);// Wild pointer
//printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));// Random value
printf("%d\n", strlen(&arr + 1));// Random value -6
printf("%d\n", strlen(&arr[0] + 1));// Random value -1
return 0;
}
int main() {
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));//7
printf("%d\n", sizeof(arr + 0));//4/8
printf("%d\n", sizeof(*arr));//1
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//4/8
printf("%d\n", sizeof(&arr + 1));//4/8
printf("%d\n", sizeof(&arr[0] + 1));//4/8
//strlen Is to find the length of the string , Focus on... In the string \0, The calculation is \0 The number of characters that appear before
//strlen It's a library function , For strings only
//sizeof Only pay attention to the size of memory space , Don't care what's in the memory
//sizeof It's the operator
printf("%d\n", strlen(arr));//6
printf("%d\n", strlen(arr + 0));//6
There is a problem
//printf("%d\n", strlen(*arr));
//printf("%d\n", strlen(arr[1]));
//
printf("%d\n", strlen(&arr));//6
printf("%d\n", strlen(&arr + 1));// Random value
printf("%d\n", strlen(&arr[0] + 1));//5
return 0;
}
3. Two dimensional array
int main() {
// Two dimensional array
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));//48
printf("%d\n", sizeof(a[0][0]));//4
//a[0] Is the array name of this one-dimensional array in the first line , Put it alone sizeof Inside ,a[0] Represents the first entire one-dimensional array
//sizeof(a[0]) Calculate the size of this line
printf("%d\n", sizeof(a[0]));//16
//(a[0] + 1)--->&a[0][0]+1
//a[0] Not alone in sizeof Inside , I didn't take the address
//a[0] It means the address of the first element , Is the address of the first element of the first row of this one-dimensional array
//a[0]+1 Is the address of the second element in the first line
printf("%d\n", sizeof(a[0] + 1));//4/8
printf("%d\n", sizeof(*(a[0] + 1)));//4
//a Although it is the address of a two-dimensional array , But it is not placed alone sizeof Inside , I didn't take the address
//a Represents the address of the first element , The first element of a two-dimensional array is its first line ,a It's the address on the first line
//a+1 Just skip the first line , Indicates the address of the second line
printf("%d\n", sizeof(a + 1));//4/8
//*(a + 1) Is the dereference of the address in the second line , I got the second line
//*(a+1)--->a[1]
//sizeof(*(a+1))--->sizeof(a[1])
printf("%d\n", sizeof(*(a + 1)));//16
//&a[0]-- Address the array name in the first row , Which is the address in the first line
//&a[0]+1-- What you get is the address on the second line
printf("%d\n", sizeof(&a[0] + 1));//4/8
printf("%d\n", sizeof(*(&a[0] + 1)));//16
//a Represents the address of the first element , It's the address on the first line
//*a Is the dereference of the address in the first line , What you get is Yixing
printf("%d\n", sizeof(*a));//16
printf("%d\n", sizeof(a[3]));//16
return 0;
}
summary :
The meaning of array names :
- sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
- & Array name , The array name here represents the entire array , It takes out the address of the entire array .
- In addition, all array names represent the address of the first element .
Two 、 Pointer written test questions
1. Interview questions 1
int main()
{
int a[5] = {
1, 2, 3, 4, 5 };
int* ptr = (int*)(&a + 1);//&a The type is int(*)[5], So you need to force the type to be converted to int*
printf("%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
// What is the result of the program ?
2. Interview questions 2
// Because I haven't learned the structure yet , The size of the structure is 20 Bytes
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);//0x00100014
//0x100000+20--->//0x100014
printf("%p\n", (unsigned long)p + 0x1);//0x00100001
//1,048,576+1--->1,048,577
//0x100001
printf("%p\n", (unsigned int*)p + 0x1);//0x00100004
//0x100000+4--->//0x100004
return 0;
}
3. Interview questions 3
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;
}
4. Interview questions 4
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int* p;
p = a[0];//a[0] Is the array name in the first row
//a[0] Represents the address of the first element , namely a[0][0] The address of ,&a[0][0]
printf("%d", p[0]);
return 0;
}
5. Interview questions 5
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]);//-4 -4
// The value of address subtraction is the number of intermediate elements
return 0;
}
6. Interview questions 6
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;
}
7. Interview questions 7
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
8. Interview questions 8
int main()
{
char* c[] = {
"ENTER","NEW","POINT","FIRST" };
char** cp[] = {
c + 3,c + 2,c + 1,c };
char*** cpp = cp;
printf("%s\n", **++cpp);//point
printf("%s\n", *-- * ++cpp + 3);//er
printf("%s\n", *cpp[-2] + 3);// st
printf("%s\n", cpp[-1][-1] + 1);//ew
return 0;
}
What we should pay attention to here is ++cpp Yes, it will cpp The address of has changed
summary
Tips : Here is a summary of the article :
for example : That's what we're going to talk about today , This article only briefly introduces the analysis of interview questions about arrays and pointers , This article is a little too much , We need to understand it slowly , You can talk about me privately if you don't understand !!!
边栏推荐
- Circuit breaker: use of hystrix
- 让学指针变得更简单(三)
- Mex related learning
- Data governance: metadata management
- Description of octomap averagenodecolor function
- 07- [istio] istio destinationrule (purpose rule)
- Nacos Development Manual
- 将 NFT 设置为 ENS 个人资料头像的分步指南
- A Closer Look at How Fine-tuning Changes BERT
- CAD ARX gets the current viewport settings
猜你喜欢
【Redis】NoSQL数据库和redis简介
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
Golang DNS 随便写写
C语言 - 位段
MEX有关的学习
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
It's hard to find a job when the industry is in recession
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
随机推荐
灰度升级 TiDB Operator
Entity class design for calculating age based on birthday
Webrtc series-h.264 estimated bit rate calculation
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
MFC 给列表控件发送左键单击、双击、以及右键单击消息
Understanding of law of large numbers and central limit theorem
23. Update data
Golang DNS 随便写写
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
(lightoj - 1410) consistent verbs (thinking)
"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
Hill sort c language
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
Easy to use tcp-udp_ Debug tool download and use
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
It's hard to find a job when the industry is in recession
MES, APS and ERP are essential to realize fine production
shu mei pai
Onie supports pice hard disk
07- [istio] istio destinationrule (purpose rule)