当前位置:网站首页>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 !!!
边栏推荐
- flask返回文件下载
- WebRTC系列-H.264预估码率计算
- Wireshark grabs packets to understand its word TCP segment
- Redis list detailed explanation of character types yyds dry goods inventory
- [Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
- 07- [istio] istio destinationrule (purpose rule)
- Onie supports pice hard disk
- [untitled]
- Solution: intelligent site intelligent inspection scheme video monitoring system
- 使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
猜你喜欢
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
A Closer Look at How Fine-tuning Changes BERT
Document 2 Feb 12 16:54
数据治理:主数据的3特征、4超越和3二八原则
Sanzi chess (C language)
esRally国内安装使用避坑指南-全网最新
[research materials] 2021 China online high growth white paper - Download attached
Epoll and IO multiplexing of redis
Machine learning - decision tree
2.10transfrom attribute
随机推荐
The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
MEX有关的学习
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
[非线性控制理论]9_非线性控制理论串讲
【云原生】手把手教你搭建ferry开源工单系统
MFC 给列表控件发送左键单击、双击、以及右键单击消息
Parameter self-tuning of relay feedback PID controller
使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
Onie supports pice hard disk
Document 2 Feb 12 16:54
49. Sound card driven article collection
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
Golang DNS 随便写写
File upload of DVWA range
Interview Reply of Zhuhai Jinshan
CAD ARX 获取当前的视口设置
Erc20 token agreement
Nacos Development Manual
从 SQL 文件迁移数据到 TiDB
PHP - Common magic method (nanny level teaching)