当前位置:网站首页>[c voice] explain the advanced pointer and points for attention (2)
[c voice] explain the advanced pointer and points for attention (2)
2022-07-02 15:06:00 【Ahao_ te】
List of articles
Preface : This section is next to 《 Explain the initial and advanced levels of the pointer and points for attention (1)》,
At last, I got to know the function pointer , Next, we will continue to start around the function pointer .
One 、 Let's talk about function pointers
As we know before, if you want to get the address of the function , There are two ways , One is to use the function name directly ( Such as test), One is to use address symbols ( Such as &test).
So how to store addresses with pointers ?
Let's take a look at the code
int test(const char* str)
{
printf("test()\n");
return 0;
}
int main()
{
printf("%p\n", test);
printf("%p\n", &test);
// With a pointer pf Deposit test Address
int (*pf)(const char*) = test;
// perhaps
int (*pf)(const char*) = &test;
// perhaps
int pf(const char*) = &test;
// perhaps
int (*****pf)(const char*) = &test;
// It can be called in the following ways .
(*pf)("abc");
test("abc");
pf("abc");
return 0;
}
We found that :
- Pointer stores function address , There are two ways to express function addresses , The pointer does not need to add * Or add more *( Add * You need to put parentheses around it ).
- image int (* )(const char *) This , We call it function pointer type .
- Because pointers and function names can represent function addresses , In the call , It can be directly through such as pf(“abc”),test(“abc”) Call directly .
So what's the use of function pointers ?
Now look at a piece of code
void menu()
{
printf("*****************************\n");
printf("***** 1.add 2.sub *****\n");
printf("***** 3.mul 2.div *****\n");
printf("***** 0.exit *****\n");
printf("*****************************\n");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
void calc(int (*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf(" Please enter 2 Operands :>");
scanf("%d %d", &x, &y);
ret = pf(x, y);
printf("%d\n", ret);
}
int main()
{
int input = 0;
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 1:
calc(Add);
// If it is not established by function pointer calc function , Then the code will be written as follows and become very redundant .
//int x = 0;
//int y = 0;
//int ret = 0;
//printf(" Please enter 2 Operands :>");
//scanf("%d %d", &x, &y);
//ret = Add(x, y);
//printf("%d\n", ret);
break;
case 2:
calc(Sub);
//int x = 0;
//int y = 0;
//int ret = 0;
//printf(" Please enter 2 Operands :>");
//scanf("%d %d", &x, &y);
//ret = Sub(x, y);
//printf("%d\n", ret);
break;
case 3:
calc(Mul);
//..
break;
case 4:
calc(Div);
//..
break;
case 0:
printf(" Exit calculator \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
Using function pointers , In some special cases , Simplify the code .
After understanding the function pointer , Let's look at two interesting pieces of code , See what these two pieces of code are for .
// Code 1
(*(void (*)())0)();
// Code 2
void (*signal(int , void(*)(int)))(int);
- In the first piece of code , First of all ( void (*)() ),( type ) Type in parentheses means strong conversion , take 0 Strong conversion to function pointer ,* Dereference address , So the essence is a function call , Call address is 0 Function at .
- From the outside ,void (* )(int) Is a function pointer type , Inside signal(int, void()(int)), function signal, Parameter is (int, void()(int)), The type is void(* )(int), There are return types , There are parameter types , Is a function declaration .
Two 、 Function pointer array
seeing the name of a thing one thinks of its function , It's an array with function pointers .
So how to express ?
The function pointer is like this
int (*pf)();
We learned pointer array before , Know that means
int* arr[];
So together , The array of function pointers is represented in this way ,pf The first and [] The combination description is an array , The type is int (*)() A function pointer .
int (*pf[])();
So what's the use of function pointer arrays ?
Look at this code
void menu()
{
printf("*****************************\n");
printf("***** 1.add 2.sub *****\n");
printf("***** 3.mul 2.div *****\n");
printf("***** 0.exit *****\n");
printf("*****************************\n");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
int ret = 0;
int (*pfArr[])(int, int) = {
0,Add,Sub,Mul,Div };
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
if (input == 0)
{
printf(" Exit calculator \n");
}
else if (input >= 1 && input <= 4)
{
printf(" Please enter 2 Operands :>");
scanf("%d%d", &x, &y);
ret = (*pfArr[input])(x, y);// Subscript access function name , Function name () Call the function in this form
printf("%d\n", ret);
}
else
{
printf(" Wrong choice \n");
}
} while (input);
return 0;
}
Access functions by accessing array subscripts , More beautification , Convenient for our code design .
Function pointer array pointer
Of course, the function pointer array can also be put into the pointer through the address of the array , Here is just to meet , You can also go down with a wireless doll , But that's it .
How to say ?
// Function pointer array
int (*pfun[5])();
// Function pointer array pointer
//(*ppfun) First explain ppfun It's a pointer , After with [] The combination description points to a 5 Array of elements ,
// The element type in the array is int (*)()
int (*(*ppfun)[5]))()
3、 ... and 、 Callback function
What is a 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
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 controlled by this function
The implementer of directly calls , It's called by another party when a particular event or condition occurs , Used to enter the event or condition
Row response .
Suppose there is a function F1 I called the function F2 When , function F1 Given by parameters F2 Passed another function F3 The pointer to , In function F2 During execution , function F2 I called the function F3, This action is called callback , It is first passed as a pointer 、 The function called back later F3 It's a callback function .
Understand as follows :
F3 The address is passed as a parameter to F2, Store F3 The pointer of the address is used to call the original F3 function , Just say F3 It's a callback function .
But it's worth it Be careful Yes. , It's not through F3 Function calls directly ( Not directly called by the implementer ), But through a storage F3 Function pointer to call .
int F3()
{
return 0;
}
//F2 I called the function F3
int F2(int (*F3)())
{
F3();// And through the F3 Visited F3 function
return 0;
}
int F1()
{
// function F1 call F2 Pass the F3 The pointer
F2(F3)
return 0;
}
Next, let's look at the callback function qsort The implementation of the .
adopt MSDN, We see some qsort Information about ,qsort It is realized by referring to the fast scheduling , We won't discuss how to realize it here , We learn how to use it .
Let's look at the function declaration .
By simplifying the function declaration, we get :
void qsort(void* base, // You need to sort the starting position of the data
size_t num, // The number of data elements to be sorted
size_t width, // The size of the data elements to be sorted ( Unit is byte )
int (*cmp)(const void* e1, const void* e2) // A function pointer - Comparison function
);
Because we don't know what kind of data to sort , So when defining the starting position , I chose void* type , How to understand it ?
Look at the code
int main()
{
int a = 10;
//char* pa = &a;//int*
void* pv = &a;//void* Is a pointer without a specific type , Can accept any type of address
//void* Is a pointer without a specific type , Therefore, you cannot dereference the operation , Also can not +- Integers
//
return 0;
}
Now let's try sorting
Look at the code
// Integer sort
// Callback function
int cmp_int(const void* e1, const void* e2)
{
// What type of data is excluded , First, try to turn around
return (*(int*)e1 - *(int*)e2);
}
void test1()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);// result {0,1,2,3,4,5,6,7,8,9}
}
}
// Structure ordering
int cmp_stu_name(const void* e1, const void* e2)
{
return strcmp(((struct stu*)e2)->name, ((struct stu*)e1)->name);
}
int cmp_stu_age(const void* e1, const void* e2)
{
return (((struct stu*)e1)->age -((struct stu*)e2)->age);
}
void test2()
{
struct stu s[] = {
{
"ade",20 }, {
"qwe", 30}, {
"wer", 25} };
int sz2 = sizeof(s) / sizeof(s[0]);
qsort(s, sz2, sizeof(s[0]), cmp_stu_name);
printf("\n");
for (i = 0; i < sz; i++)
{
printf("%d ", (s + i)->name);
}
}
void test3()
{
struct stu s[] = {
{
"ade",20 }, {
"qwe", 30}, {
"wer", 25} };
int sz2 = sizeof(s) / sizeof(s[0]);
qsort(s, sz2, sizeof(s[0]), cmp_stu_age);
printf("\n");
for (i = 0; i < sz; i++)
{
printf("%d ", (s + i)->age);
}
}
int main()
{
test1();
test2()
test3()
return 0;
}
边栏推荐
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- 【题解】Educational Codeforces Round 82
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- 求轮廓最大内接圆
- 数据库连接池和数据源
- CDN 在游戏领域的应用
- MathML to latex
- C#延时、在线程中开启定时器、获取系统时间
- Slashgear shares 2021 life changing technology products, which are somewhat unexpected
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
猜你喜欢
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
[email protected] : The platform “win32“ is incompatible with this module."/>
info [email protected] : The platform “win32“ is incompatible with this module.
IE 浏览器正式退休
[apipost] tutorial
LeetCode 2310. 个位数字为 K 的整数之和
复用和分用
forEach的错误用法,你都学废了吗
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
C#代码审计实战+前置知识
socket(套接字)与socket地址
随机推荐
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
STM32 standard firmware library function name (I)
求轮廓最大内接圆
记一次报错解决经历依赖重复
广州市应急管理局发布7月高温高湿化工安全提醒
C语言中的算术运算及相关练习题
Mavn 搭建 Nexus 私服
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
MFC console printing, pop-up dialog box
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Wechat applet uses towxml to display formula
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
Niuke Practice 101
LeetCode - 搜索二维矩阵
jmeter脚本参数化
天猫商品详情接口(APP,H5端)
kibana 基础操作
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
关于网页中的文本选择以及统计选中文本长度