当前位置:网站首页>Advanced level of C language pointer (Part 1)
Advanced level of C language pointer (Part 1)
2022-07-01 08:37:00 【Stir fried rice with lotus root】
Blogger : Stir fried lotus root bibimbap
Blog :(10 Bar message ) The blog of stir fried lotus root bibimbap _CSDN Blog -C Language bloggers
special column :(10 Bar message ) C Language _ The blog of stir fried lotus root bibimbap -CSDN Blog
Column introduction : Contains from C Explain the knowledge points from the beginning to the advanced level of the language , use C Language to write a variety of small programs , Generally around C Basic column ;
* Key points of this blog ️
1. Character pointer
2. Array pointer
3. Pointer array
4. Array parameter passing and pointer parameter passing
5. A function pointer
Foreword review :
1. A pointer is a variable , It's used to store the address , The address uniquely identifies a piece of memory space .
2. The size of the pointer is fixed 4/8 Bytes (32 Bit platform /64 Bit platform ).
3. Pointers are typed , The type of pointer determines the type of pointer +- Integer step size , The permission of pointer dereference operation . 4. The operation of the pointer .
1. Character pointer
We should have seen it before char* Character pointer , Generally, the following use cases occur :
int main()
{
char ch = 'w';
char *pc = &ch;
*pc = 'w';
return 0;
}int main()
{
const char* pstr = "hello";// Here is to put a string into pstr Is it in the pointer variable ?
printf("%s\n", pstr);
return 0;
}

The result of printing is hello, Is that pstr The string stored inside hello Well ? Never think so , Pointer variables store addresses ,pstr It should be said that it is the address where the first element of the string is stored ,printf Print will be printed according to this address , encounter \0 Will stop printing ;
The above code is clear. Let's take a look at the following code :
#include <stdio.h> int main() { char str1[] = "hello bit."; char str2[] = "hello bit."; const char *str3 = "hello bit."; const char *str4 = "hello bit."; if(str1 ==str2) printf("str1 and str2 are same\n"); else printf("str1 and str2 are not same\n"); if(str3 ==str4) printf("str3 and str4 are same\n"); else printf("str3 and str4 are not same\n"); return 0; }Ask what the print result is ?
Answer key :
First understand str1==str2 and str3==str4 What does that mean? , This expression actually means to compare whether their addresses are the same , that str1 and str2 They are two different arrays , How could the address be the same ? and str3 and str4 They are all character constants , There is no need to create new space in memory , Their first addresses all point to characters ‘h’ Of , So the address must be the same ;
2. Pointer array
A pointer array is an array of pointer types , The elements in it are all pointer types ;
int* arr1[10]; // An array of integer pointers
char *arr2[4]; // An array of first-order character pointers
char **arr3[5]; // An array of secondary character pointers
Priority understanding :arr1 The first and [ 10 ] combination , It shows that this is an array , The type of array is before it int *, This is a pointer array , Used to store pointers ;
3. Array pointer
3.1 Definition of array pointer
What are array pointers ?
It's a pointer , Pointer to array , We already know there are int *pa Integer pointer , float *pb Floating point pointer , They are all pointers to a certain type , So is the array pointer , The type it points to is an array ;
int * arr1[10] = { 0 };
int (* arr2)[10] = { 0 }; //arr1 and arr2 What are the differences ?
arr1 We've already done that , It is an array of pointers ;
arr2 Pay attention to its (*arr2), This means that in any case arr2 Has been and * Combined with the , This shows that arr2 It's the pointer, not the array name , Because it has no chance to [10] To combine , The type pointed to is int [10], So we call it array pointer ;
Be careful : because [ ] The priority ratio * high ,arr2 I'll start with [10] combination , So if you want an array pointer, you must add () Give Way * and arr2 First combine , In this way, we can guarantee arr2 It's an array pointer ;
3.2 & Array name VS Array name
int arr[10] = { 0 };
&arr and arr Is there anything different ? It seems to be exactly the same ,arr Is the address of the first element of the array &arr It is also the first element address , It's the same. Why bother to use it & Well ?
My answer is NO! Programming is rigorous , So its grammar is even more so , Just because the contents are the same doesn't mean they have the same meaning , Without proof, I will take you to study &arr and arr The difference between ;
We print their addresses exactly the same
#include <stdio.h> int main() { int arr[10] = {0}; printf("%p\n", arr); printf("%p\n", &arr); return 0; }Running results :
But let's look at the following code :
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
printf("arr = %p\n", arr);
printf("arr+1 = %p\n", arr + 1);
printf("&arr= %p\n", &arr);
printf("&arr+1= %p\n", &arr + 1);
return 0;
}Running results :

What does this ?&arr and arr It's worth the same , But they certainly have different meanings , So some things we should not be confused by appearances , If there is any doubt about the matter, let's get to the bottom of it and kill it ; in fact &arr Is the address representing all elements of the array , But there must be an address to express them , But the space is limited, it is impossible to express all the element addresses , So we still use the first element of the array to represent them , But its meaning is different ;
reflection :
&arr You can think about what it is ? Is it an array pointer , Yes indeed ,&arr The point is arr Entire array , In this case, its type is int (*)[10];
Address of array +1, Skip the size of the entire array , therefore &arr+1 be relative to &arr The difference is 40.
3.3 The use of array pointers
Since there is an array pointer, how should we use it ?
chestnuts 1( Not commonly used ):
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,0};
int (*p)[10] = &arr;// Put the array arr To an array pointer variable p
// But we seldom write code like this
return 0;
}chestnuts 2( Commonly used ):
#include <stdio.h>
void print_arr1(int arr[3][5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void print_arr2(int(*arr)[5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][5] = { 1,2,3,4,5,6,7,8,9,10 };
print_arr1(arr, 3, 5);
// Array name arr, Represents the address of the first element
// But the first element of a two-dimensional array is the first row of a two-dimensional array
// So the message here is arr, It's actually equivalent to the address on the first line , Is the address of a one-dimensional array
// You can use an array pointer to receive
print_arr2(arr, 3, 5);
return 0;
}
After learning pointer array and array pointer, let's review and see what the following code means :
int arr[5]; // integer array
int *parr1[10]; // Pointer array
int (*parr2)[10]; // Array pointer
int (*parr3[10])[5]; // An array that holds array pointers
4. Array parameters , Pointer parameter
When writing code, it is inevitable to 【 Array 】 perhaps 【 The pointer 】 Pass to function , How to design the parameters of the function ?
4.1 One dimensional array parameters
#include <stdio.h>
void test(int arr[])//ok
{}
void test(int arr[10])//ok
{}
void test(int *arr)//ok
{}
void test2(int *arr[20])//ok
{}
void test2(int **arr)//ok
{}
int main()
{
int arr[10] = {0};
int *arr2[20] = {0};
test(arr);
test2(arr2);
return 0;
}4.2 Two dimensional array parameters
void test(int arr[3][5])//ok?
{}
void test(int arr[][])//ok?
{}
void test(int arr[][5])//ok?
{}
// summary : Two dimensional array parameters , Function parameter design can only omit the first [] The number of .
// Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row .
// So it's easy to calculate .
void test(int *arr)//ok?
{}
void test(int* arr[5])//ok?
{}
void test(int (*arr)[5])//ok?
{}
void test(int **arr)//ok?
{}
int main()
{
int arr[3][5] = {0};
test(arr);
}
4.3 First level pointer parameter transfer
#include <stdio.h>
void print(int *p, int sz)
{
int i = 0;
for(i=0; i<sz; i++)
{
printf("%d\n", *(p+i));
}
}
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9};
int *p = arr;
int sz = sizeof(arr)/sizeof(arr[0]);
// First level pointer p, Pass to function
print(p, sz);
return 0;
}
When the parameter part of a function is a first-order pointer , What parameters can a function take ?
4.4 The secondary pointer transmits parameters
#include <stdio.h>
void test(int** ptr)
{
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int*p = &n;
int **pp = &p;
test(pp);
test(&p);
return 0;
}When the parameter of the function is a secondary pointer , What parameters can be received ?
边栏推荐
- Field agricultural irrigation system
- Burpsuite -- brute force cracking of intruder
- [JS reverse] MD5 encryption parameter cracking
- MAVROS发送自定义话题消息给PX4
- MATLAB小技巧(23)矩阵分析--模拟退火
- Principle and application of single chip microcomputer - off chip development
- 01 numpy introduction
- factory type_ Id:: create process resolution
- Leetcode t39: combined sum
- Brief introduction to AES
猜你喜欢

【面试必刷101】链表

《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断

避免按钮重复点击的小工具bimianchongfu.queren()

一文纵览主流 NFT 市场平台版税、服务费设计

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

《单片机原理及应用》—定时器、串行通信和中断系统

Field agricultural irrigation system

Principle and application of single chip microcomputer - principle of parallel IO port

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates

SPL installation and basic use (II)
随机推荐
Li Kou 1358 -- number of substrings containing all three characters (double pointer)
机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现
In depth learning training sample amplification and tag name modification
如何招到适合自己店铺的淘宝主播
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
P4 installation bmv2 detailed tutorial
Data analysis notes 11
Leetcode t34: find the first and last positions of elements in a sorted array
機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
Manually dig XSS vulnerabilities
Model and view of QT
一文纵览主流 NFT 市场平台版税、服务费设计
[深度剖析C语言] —— 数据在内存中的存储
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
你了解数据是如何存储的吗?(C整型和浮点型两类)
为什么LTD独立站就是Web3.0网站!
挖财打新股安全吗
【js逆向】md5加密参数破解
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
《单片机原理及应用》-片外拓展

