当前位置:网站首页>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 !!!
边栏推荐
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
- Circuit breaker: use of hystrix
- Epoll and IO multiplexing of redis
- 【T31ZL智能视频应用处理器资料】
- Notes on software development
- 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
- P3047 [usaco12feb]nearby cows g (tree DP)
- A Closer Look at How Fine-tuning Changes BERT
- [KMP] template
猜你喜欢

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

ESP系列引脚說明圖匯總

hcip--mpls

Make learning pointer easier (3)
![[nonlinear control theory]9_ A series of lectures on nonlinear control theory](/img/a8/03ed363659a0a067c2f1934457c106.png)
[nonlinear control theory]9_ A series of lectures on nonlinear control theory

Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University

A Closer Look at How Fine-tuning Changes BERT

National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund

What is the use of entering the critical point? How to realize STM32 single chip microcomputer?

Hcip day 16
随机推荐
Helm install Minio
[KMP] template
flask返回文件下载
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
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
Description of octomap averagenodecolor function
好用的TCP-UDP_debug工具下载和使用
数据治理:元数据管理篇
A Closer Look at How Fine-tuning Changes BERT
PHP - Common magic method (nanny level teaching)
ESP series pin description diagram summary
从 SQL 文件迁移数据到 TiDB
使用 TiUP 升级 TiDB
Erc20 token agreement
Pangolin Library: control panel, control components, shortcut key settings
23. Update data
esRally国内安装使用避坑指南-全网最新
【T31ZL智能视频应用处理器资料】
Nacos Development Manual
ESP系列引脚說明圖匯總