当前位置:网站首页>Advanced pointer practice
Advanced pointer practice
2022-07-28 07:27:00 【Four years later.】
Pointer practice
List of articles
One dimensional array
// One dimensional array
int a[] = {
1,2,3,4};
printf("%d\n",sizeof(a));【16】---- special case : The array list is unique sizeof Internal time , It calculates the size of the entire array
printf("%d\n",sizeof(a+0));【4/8】---- The array name is the address of the first element , stay a+0 It's in the array 1 The address of , because a+0 It's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(*a));【4】---- The array name is the address of the first element ,* It's the first element , So we calculate the size of the first element
printf("%d\n",sizeof(a+1));【4/8】---- The array name is the address of the first element ,a+1 It's in the array 2 The address of , because a+1 It's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(a[1]));【4】----a[1] Is the second element in the array , So we calculate the size of the second element
printf("%d\n",sizeof(&a));【4/8】----&a What you get is the address of the whole array , but &a It's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(*&a));【16】---- Take out the address of the whole array , Dereference the address of the entire array
because 8a Medium a Is the address of the first element of the array ,a<==>&a[0],*&a[0] amount to a[0], This is equivalent to * and & Offset each other
printf("%d\n",sizeof(&a+1));【4/8】----&a+1 What you get is an array a The address of the back position , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(&a[0]));【4/8】----&a[0] What you get is the address of the first element , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(&a[0]+1));【4/8】----&a[0]+1 What you get is the address of the second element , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
A character array
## A character array
char arr[] = {
'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));【6】---- special case :arr The array list is placed alone in sizeof in , What you get is the size of the entire array
printf("%d\n", sizeof(arr+0));【4/8】---- The array name is the first element address ,arr+0 What you get is the address of the first element , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n", sizeof(*arr));【1】---- The array name is the address of the first element ,*arr What you get is the first element ,sizeof(char) yes 1 Bytes
printf("%d\n", sizeof(arr[1]));【1】----arr[1] Is the size of the second element
printf("%d\n", sizeof(&arr));【4/8】---- It takes out the address of the entire array , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n", sizeof(&arr+1));【4/8】----&arr What you get is the address of the whole array ,&arr+1 It refers to the address of the space behind the array , Because it's the address , therefore sizeof( Address ) Get is 4/8 Bytes
printf("%d\n", sizeof(&arr[0]+1));【4/8】----&arr[0] What you get is the address of the first element of the array ,&arr[0]+1 Points to the address of the second element of the array , Because it's the address , therefore sizeof( Address ) Get is 4/8 Bytes
About strlen
char arr[] = {
'a','b','c','d','e','f'};
printf("%d\n", strlen(arr));【 Random value 】 Because there is no '\0' The existence of , So when calculating the length , What you get is a random value
printf("%d\n", strlen(arr+0));【 Random value 】arr Is the address of the first element of the array ,arr+0 What you get is also the address of the first element of the array , Because there is no '\0' The existence of , So when calculating the length , What you get is a random value
printf("%d\n", strlen(*arr));【 Compile error 】----*arr Get the first character 'a',strlen(97), Pass to strlen Should be the address , Dereference to get a in the future ,a The address of is a wild pointer
printf("%d\n", strlen(arr[1]));【 Compile error 】----arr[1] What you get is the second character 'b' Pass to strlen Should be the address , Dereference to get b in the future ,b The address of is a wild pointer
printf("%d\n", strlen(&arr));【 Random value 】----&arr What you get is the address of the entire array , Because there is no '\0' The existence of , So when calculating the length , What you get is a random value
printf("%d\n", strlen(&arr+1));【 Random value -6】----&arr What you get is the address of the entire array ,&arr+1 It refers to the address of the space behind the character array , So when calculating the length , What you get is a size after subtracting the length of the character array , That is, random value -6
printf("%d\n", strlen(&arr[0]+1));【 Random value -1】----&arr[0] What you get is the address of the first element of the array ,&arr[0]+1 What you get is the address of the second element , So when calculating the length , What you get is the size of the length after subtracting the first character , That is, random value -1
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));【7】----arr Put it alone sizeof On the inside , It calculates the size of the entire array , because sizeof The calculation is the size of the occupied space , So it also includes '\0' So what we get is 7
printf("%d\n", sizeof(arr+0));【4/8】----arr Is the address of the first element of the array ,arr+0 What you get is the address of the first element of the array , Because it's the address , therefore sizeof( Address ) The resulting size is 4/8
printf("%d\n", sizeof(*arr));【1】----*arr It's the first element , the reason being that char type , So it is 1
printf("%d\n", sizeof(arr[1]));【1】----arr[1] Is the second element in the array , the reason being that char type , So it is 1
printf("%d\n", sizeof(&arr));【4/8】----&arr It's the size of the whole array , Because it's the address , therefore sizeof( Address ) The resulting size is 4/8 Bytes
printf("%d\n", sizeof(&arr+1));【4/8】----&arr+1 It points to the address of the space behind the array , Because it's the address , therefore sizeof( Address ) The size obtained is 4/8 Bytes
printf("%d\n", sizeof(&arr[0]+1));【4/8】----&arr[0]+1 What you get is the address of the second element , Because it's the address , therefore sizeof( Address ) The size obtained is 4/8 Bytes
printf("%d\n", strlen(arr));【6】----strlen The calculation is '\0' The number of previous elements , Yes 6 individual
printf("%d\n", strlen(arr+0));【6】----strlen The calculation is '\0' The number of previous elements , Yes 6 individual
printf("%d\n", strlen(*arr));【 Compile error 】*arr Get the first character 'a',strlen(97), Pass to strlen Should be the address , Dereference to get a in the future ,a The address of is a wild pointer
printf("%d\n", strlen(arr[1]));【 Compile error 】----arr[1] What you get is the second character 'b' Pass to strlen Should be the address , Dereference to get b in the future ,b The address of is a wild pointer
printf("%d\n", strlen(&arr));【6】----strlen The calculation is '\0' The number of previous elements , Yes 6 individual
printf("%d\n", strlen(&arr+1));【 Random value 】---- It points to the address of the space behind the array , I don't know when I will meet '\0', So it's a random value
printf("%d\n", strlen(&arr[0]+1));【5】----strlen The calculation is '\0' The number of previous elements , Yes 5 individual

char *p = "abcdef";
printf("%d\n", sizeof(p));【4/8】---- because p Is a pointer variable , The size of the pointer variable is 4/8 Bytes , Depends on the compiler
printf("%d\n", sizeof(p+1));【4/8】---- hypothesis p The address for 0x0012ff40 that p+1 Namely 0x12ff41 Or an address , It's the address ,sizeof( Address ) The size is 4/8 Bytes
printf("%d\n", sizeof(*p));【1】---- because p yes char* Type of , Access a byte , So a byte is a, You get 1
printf("%d\n", sizeof(p[0]));【1】----p[0] <==> *(p+0) Is the first character a Size ,a Is the character , So it is 1 Bytes
printf("%d\n", sizeof(&p));【4/8】----&p What is taken out is the pointer variable p The address of , It should be saved with a secondary pointer , But the secondary pointer is also a pointer , So it is 4/8 Bytes
printf("%d\n", sizeof(&p+1));【4/8】----&p+1 Get the address , But the special thing is , What you get is a pointer variable p skip p Variable space , The address of the space behind , It's the address , still 4/8 Bytes
printf("%d\n", sizeof(&p[0]+1));【4/8】----&p[0] yes a The address of ,&p[0]+1 Get is b The address of , It's the address , Namely 4/8 Bytes
printf("%d\n", strlen(p));【6】----p It's in there a The address of , So pass it on to strlen Of course a The address of ,strlen The calculation is '\0' The number of previous elements , Yes 6 individual
printf("%d\n", strlen(p+1));【5】----p It's in there a The address of ,p+1 Get is b The address of , So pass it on to strlen Of course b The address of ,strlen The calculation is '\0' The number of previous elements , Yes 5 individual
printf("%d\n", strlen(*p));【 Compile error 】----*arr Get the first character 'a',strlen(97), Pass to strlen Should be the address , Dereference to get a in the future ,a The address of is a wild pointer
printf("%d\n", strlen(p[0]));【 Compile error 】----*arr Get the first character 'a',strlen(97), Pass to strlen Should be the address , Dereference to get a in the future ,a The address of is a wild pointer
printf("%d\n", strlen(&p));【 Random value 】----&p Get is p The address of , And the string is not a space at all , I don't know when I will meet \0 stop , So it's a random value
printf("%d\n", strlen(&p+1));【 Random value 】----&p+1 Get is p add 1 The address of , And the string is not a space at all , I don't know when I will meet \0 stop , So it's a random value
printf("%d\n", strlen(&p[0]+1));【5】----&p[0] Get is a The address of ,a The address of +1 Get is b The address of ,trlen The calculation is '\0' The number of previous elements , Yes 5 individual
Two dimensional array

// Two dimensional array
int a[3][4] = {
0};
printf("%d\n",sizeof(a));【48】---- The array list is placed alone in sizeof Inside , It calculates the size of the entire array So it is 48
printf("%d\n",sizeof(a[0][0]));【4】----arr[0][0] Is the first column element of the first row of the array , yes int Type of , So it is 4
printf("%d\n",sizeof(a[0]));【16】---- When we access a two-dimensional array ,arr[0][j] j The value range of is 0-3 When accessing a one-dimensional array ,j The value range of is 0-9 Two dimensional array arr[0] arr[1] arr[2] Is the array name of each row of the two-dimensional array
The array list is placed alone in sizeof Inside , It calculates the size of all elements in that line , So it is 16
printf("%d\n",sizeof(a[0]+1));【4/8】----a[0] <==>&a[0][0],&a[0][0]+1 What you get is the address of the second element in the first line , Because it's the address , therefore sizeof( Address ) The size is 4/8 Bytes
printf("%d\n",sizeof(*(a[0]+1)));【4】----a[0] <==>&a[0][0],&a[0][0]+1 What you get is the address of the second element in the first line , Because it's the address , Dereference it , What you get is int The size of the type

printf("%d\n",sizeof(a+1));【】----a Is the array name of a two-dimensional array ,a Represents the address of the first element , The first element of a two-dimensional array is its first row ,a It's the address on the first line , Address of array +1 What you get is the address on the second line , Because it's the address , So it is 4/8 Bytes
printf("%d\n",sizeof(*(a+1)));【16】----*(a+1) <==> a[1] therefore sizeof(*(a+1)) <==> sizeof(a[1]),a[1] Is the array name in the second line , Calculate the size of the second line
printf("%d\n",sizeof(&a[0]+1));【4/8】----&a[0] I got the address on the first line ,&a[0]+1 What you get is the address of the element in the second line , Because it's the address , So it is 4/8 Bytes
printf("%d\n",sizeof(*(&a[0]+1)));【4】----&a[0] I got the address on the first line ,&a[0]+1 What you get is the address of the element in the second line , Dereference it , What you get is 4
printf("%d\n",sizeof(*a));【16】----a Is the address of the first element , The first element of a two-dimensional array is the first row , Dereference the address in the first line , What you get is the first line , The first line is placed separately sizeof Inside , What you get is 16
printf("%d\n",sizeof(a[3]));【16】---- Although there is no third line , But it will be analyzed according to the type , obtain 16
summary : The meaning of array names :
1. sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
2. & Array name , The array name here represents the entire array , It takes out the address of the entire array .
3. In addition, all array names represent the address of the first element .
边栏推荐
- OJ questions about fast and slow pointers in linked lists
- Shell --- conditional statement practice
- cdn.jsdelivr.net不可用,该怎么办
- Shortest seek time first (SSTF)
- 232 (female) to 422 (male)
- 合并两个排序的链表——每日两题
- 【着色器实现Negative反色效果_Shader效果第十一篇】
- Shell--- sed statement exercise
- Date n days ago
- Review of C language (variable parameters)
猜你喜欢

Branch and loop statements

Guava cache of guava

Basic knowledge of functions and special points

Big talk persistence and redolog

再次出现用户净流失,大失颜面的中国移动推出超低价套餐争取用户

Soft exam certificate can be used like this! Get a certificate = get a professional title?

删除链表中的节点——每日一题

Redis主从复制原理及配置

Detailed explanation of active scanning technology nmap

教程篇(7.0) 06. 零信任网络访问ZTNA * FortiClient EMS * Fortinet 网络安全专家 NSE 5
随机推荐
Install pycharm
VCF file production
【无标题】
Qucs preliminary use guide (not Multism)
OJ questions about fast and slow pointers in linked lists
链表中倒数第k个节点——双指
caffe fine tune
Student duty problems
Easypoi export interlaced style settings
和为s的两个数字——每日两题
GFS distributed file system
用户态vs内核态、进程vs线程
PyTorch - Dropout: A Simple Way to Prevent Neural Networks from Overfitting
Basic knowledge of video format: let you know MKV, MP4, h.265, bit rate, color depth, etc
[shaders realize negative anti color effect _shader effect Chapter 11]
Heroku operation summary
High response ratio first
Redis的RDB持久化和AOF持久化的底层原理
Redis哨兵模式及集群
List of papers on gestures