当前位置:网站首页>C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
2022-07-06 10:39:00 【YRedd22】
Important concepts of pointer :
A pointer is a variable , It's used to store the address , The address uniquely identifies a piece of memory space .
The size of the pointer is fixed 4/8 Bytes (32 Bit platform /64 Bit platform ).
Pointers are typed , The type of pointer determines the type of pointer +- Integer step size , The permission of pointer dereference operation .
The operation of the pointer .
1. Character pointer


Code char* pstr = "hello bit."; It's very easy for students to think it's a string hello bit Put it in the character pointer pstr in
了 , however / The essence is to put the string hello bit. The address of the first character is put in pstr in .


2. Pointer array

Take a look at the application of several pointer arrays :



3. Array pointer
Array pointers are pointers


There are two exceptions when the array name is the first element address :



If the address passed by the parameter is :

The use of array pointers :

// 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
Let's take a look at the meaning of the following code :

4. Array parameters 、 Pointer parameter
4.1 One dimensional array parameters

4.2 Two dimensional array parameters

4.3 First level pointer parameter transfer


4.4 The secondary pointer transmits parameters


5. A function pointer

The output is two addresses , These two addresses are test Address of function .
The address of our function should be saved , How to keep ?

Call an instance of a function through a function pointer :


Read two interesting pieces of code :


6. Function pointer array ‘
Let's start with a piece of code :

How to simplify ? Make it into the form of function pointer array

Using examples :
Design a counter :
1. General method :

2. adopt Function pointer array improvement A simple way :

7. A pointer to an array of function pointers
The pointer to the array of function pointers is a The pointer
The pointer points to a Array , The elements of the array are A function pointer

8. Callback function
A callback function is a function called through a function pointer . If you put a pointer to a function ( Address ) Pass as a parameter to another
A function , When this pointer is used to call the function it points to , Let's just say this is a callback function . The callback function is not created by the
The implementer of the function calls , It's called by another party when a particular event or condition occurs , For the event or
Condition response .
First demonstrate qsort Use of functions :

So let's see first void* base The role of

size_t num It represents the number of elements to be sorted
size_t width Represents the size of an element , Unit is byte
notes :size_t Represents an unsigned integer




Next we use qsort Function to write a bubble sort by yourself :
notes :Swap Notice in the function width Can't ignore , Also note that after each byte is compared, one byte must be skipped backward
Also note that this function uses the idea of callback function
9. Pointer and array written test question analysis
subject :

Running results :

Analysis of the answer :

subject :

Running results :

analysis :

subject :

Analysis of the answer :

subject :

Running results :

Analysis of the answer :
subject :

Analysis of the answer :

subject :

Running results :

Analysis of the answer :

subject :

Analysis of the answer :

subject :

Running results :

Analysis of the answer :

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 .
4.strlen What I want is an address , Count backwards from the address passed by the parameter , until \0 Location ,\0 As many characters as there are before
5. The array name of a two-dimensional array represents the address of the first element , But the address of the first element here refers to the address of the first line ;a[0] In a two-dimensional array, the array name in the first row
Example :
int a[3][4];
The array name of the two-dimensional array is a; a- The address of the first element of a two-dimensional array ( first line ) The address of
The array name in the first row is a[0]; a[0] Represents the address of the first element in the first line
The array name in the second line is a[1]; a[1] Represents the address of the first element in the second line
The array name in the third line is a[2]; a[2] Represents the address of the first element in the third line
sizeof(a) &a
sizeof(a[0]) &a[0]
sizeof(a[1]) &a[1]
sizeof(a[2]) &a[2] The array names of the above four cases all represent the whole array
10. Pointer written test questions

Analysis results :
subject :

Analysis results : It's important to be careful here +1 What on earth is it +1, Pay attention to the conversion of types after forced type conversion

subject :

Analysis results :

Running results :
subject :

Analysis results :

a[0] Is the array name in the first row , A separate a[0] Represents the address of the first element
subject :
Result analysis :
subject :
Result analysis :

subject :
Result analysis :
there char** How to understand ?
char* *pa;
In front of a char* Express ++ Will skip a char* type ,*pa Express pa It's a pointer
subject :

Result analysis :


Running results :
边栏推荐
- Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
- UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
- Mysql34 other database logs
- Time in TCP state_ The role of wait?
- MySQL24-索引的数据结构
- Nanny hand-in-hand teaches you to write Gobang in C language
- Windchill配置远程Oracle数据库连接
- Mysql27 index optimization and query optimization
- Download and installation of QT Creator
- Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
![[after reading the series] how to realize app automation without programming (automatically start Kwai APP)](/img/e1/bad9cfa70d3c533cfaddeee40b96f1.jpg)
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)

Typescript入门教程(B站黑马程序员)

MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.

实现微信公众号H5消息推送的超级详细步骤

MySQL26-性能分析工具的使用

保姆级手把手教你用C语言写三子棋

MySQL21-用户与权限管理

MySQL35-主从复制

数据库中间件_Mycat总结

Water and rain condition monitoring reservoir water and rain condition online monitoring
随机推荐
数据库中间件_Mycat总结
Emotional classification of 1.6 million comments on LSTM based on pytoch
Time in TCP state_ The role of wait?
ByteTrack: Multi-Object Tracking by Associating Every Detection Box 论文阅读笔记()
Introduction tutorial of typescript (dark horse programmer of station B)
[C language] deeply analyze the underlying principle of data storage
PyTorch RNN 实战案例_MNIST手写字体识别
The underlying logical architecture of MySQL
In fact, the implementation of current limiting is not complicated
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
UEditor国际化配置,支持中英文切换
MySQL24-索引的数据结构
Baidu Encyclopedia data crawling and content classification and recognition
MySQL27-索引优化与查询优化
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
February 13, 2022-3-middle order traversal of binary tree
评估方法的优缺点
第一篇博客
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
Pytorch RNN actual combat case_ MNIST handwriting font recognition