当前位置:网站首页>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 !!!
边栏推荐
- Make learning pointer easier (3)
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- 使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
- Easy to use tcp-udp_ Debug tool download and use
- PHP - Common magic method (nanny level teaching)
- 灰度升级 TiDB Operator
- 24. Query table data (basic)
- CAD ARX 获取当前的视口设置
- 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
- TiDB备份与恢复简介
猜你喜欢
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
21. Delete data
IP lab, the first weekly recheck
指针和数组笔试题解析
Golang DNS 随便写写
"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
ESP系列引脚說明圖匯總
Epoll and IO multiplexing of redis
Sanzi chess (C language)
随机推荐
JS select all and tab bar switching, simple comments
Binary tree creation & traversal
将 NFT 设置为 ENS 个人资料头像的分步指南
Nc204382 medium sequence
flask返回文件下载
1204 character deletion operation (2)
数据治理:误区梳理篇
How to estimate the number of threads
Risk planning and identification of Oracle project management system
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
2.10transfrom attribute
P3047 [usaco12feb]nearby cows g (tree DP)
CAD ARX 获取当前的视口设置
esRally国内安装使用避坑指南-全网最新
It's hard to find a job when the industry is in recession
数据治理:元数据管理篇
Oracle time display adjustment
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Nacos Development Manual
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data