当前位置:网站首页>Make learning pointer easier (2)
Make learning pointer easier (2)
2022-06-27 12:47:00 【Xiao Zhang, China Academy of Aeronautics and Astronautics】
List of articles
Preface
In depth study of pointer ( Two )
One 、 Function pointer array
An array is a storage space for the same type of data , So we've learned about pointer arrays , You can read my last blog if you don't understand ; Make learning pointer easier ( One )
such as :int* arr[10]; // Each element of the array is int *
So we can understand the function pointer array , The first must be an array , Second, the array element type is function pointer ( Address ); Then save the address of the function in an array , This array is called the function pointer array , How to define the array of function pointers ?
int (* parr1[10])();
int * parr2 [10] ();
int ( * )() parr3[10];
Look at these three , That's a function pointer array ? The answer is :parr1 ,parr1 The first and [] combination , explain parr1 It's an array , What is the content of the array ? yes int (*)() Function pointer of type . Note that the array name should be placed in the band * In parentheses ,int ( *parr1[10])(); So the first one is the function pointer array ; Purpose of function pointer array : Transfer table ;
1.1 The purpose of the function pointer ( The use of function pointers in the last blog )
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a*b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
do
{
printf( "*************************\n" );
printf( " 1:add 2:sub \n" );
printf( " 3:mul 4:div \n" );
printf( "*************************\n" );
printf( " Please select :" );
scanf( "%d", &input);
switch (input)
{
case 1:
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = add(x, y);
printf( "ret = %d\n", ret);
break;
case 2:
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = sub(x, y);
printf( "ret = %d\n", ret);
break;
case 3:
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = mul(x, y);
printf( "ret = %d\n", ret);
break;
case 4:
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = div(x, y);
printf( "ret = %d\n", ret);
break;
case 0:
printf(" Exit procedure \n");
breark;
default:
printf( " Wrong choice \n" );
break;
}
} while (input);
return 0;
}
Select from above 1~4 There are a lot of duplicate or very similar code , If you add more functions to the computer , You have to add a lot of code , There are a lot of the same duplicate code , We can package it into a function to implement the function, so as to avoid writing the same code repeatedly ; So each of these functions is a function , How to integrate into one function to call all function functions ?
void computer(int (*cmp)(int x,int y)){
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = cmp(x, y);
printf( "ret = %d\n", ret);
}
I think this function can solve the problem , below switch Statement to call this function directly , Just transfer the function function , One thing to note is that the return value must match , Parameters are of the same type , Here are two int Parameters of type , The return value is int type , Such a function can be passed to perform operations !!! This is a function pointer ;
1.2 Purpose of function pointer array
Or the big code above , Use a function pointer array to apply , Use arrays for those function functions , See how the code implements
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a*b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
int(*p[5])(int x, int y) = {
0, add, sub, mul, div }; // Transfer table
while (input)
{
printf( "*************************\n" );
printf( " 1:add 2:sub \n" );
printf( " 3:mul 4:div \n" );
printf( "*************************\n" );
printf( " Please select :" );
scanf( "%d", &input);
if ((input <= 4 && input >= 1))
{
printf( " Enter the operands :" );
scanf( "%d %d", &x, &y);
ret = (*p[input])(x, y);
}
else
printf( " Incorrect input \n" );
printf( "ret = %d\n", ret);
}
return 0;
}
Do not use the dereference when calling * Symbols are OK , direct p [input]) ( x , y ); This is a use of our function pointer array , Also called transfer table ;
Two , A pointer to an array of function pointers
This is the pointer , Then it saves the address of the function pointer array , This is just to understand , Know how to write , Just understand it ;
void test(const char* str)
{
printf(“%s\n”, str);
}
int main()
{
// A function pointer pfun
void ( * pfun )( const char * ) = test;
// An array of function pointers pfunArr
void ( * pfunArr[5] )( const char * str);
pfunArr[0] = test;
// Pointer to function array pfunArr The pointer to ppfunArr
void ( * ( * ppfunArr)[5] ) (const char * ) = &pfunArr;
return 0;
}
3、 ... and , 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 .
That is, we do not call this function directly , Instead, pass the function address , Call this function through the function pointer in another function , Would it be easier to understand ? We qsort A library function is a typical callback function ;
#include <stdio.h>
//qosrt The user of the function has to implement a comparison function
int int_cmp(const void * p1, const void * p2)
{
return (*( int *)p1 - *(int *) p2);
}
int main()
{
int arr[] = {
1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int i = 0;
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);
for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)
{
printf( "%d ", arr[i]);
}
printf("\n");
return 0;
}
Let's simulate the implementation qsort Library function , I have written an article about simulation implementation before qsort Function blog can see ;
qsort Simulation and implementation of library functions
summary
There is also a long topic analysis blog on the interview questions , Then the pointer series blog will come to an end for the time being ;
边栏推荐
- 行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
- ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training
- [fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year
- 大小端字节序
- MySQL高阶语句(一)
- Uniapp drop-down layer selection box effect demo (sorting)
- 解除百度文库VIP、语雀、知乎付费限制,原来这么简单
- script defer async模式
- Three traversal methods of binary tree
- 让学指针变得更简单(一)
猜你喜欢
随机推荐
Browser cookie to selenium cookie login
Failed to execute NPM instruction, prompting ssh: Permission denied
浏览器输入url地址,到页面渲染发生了什么
MySQL高阶语句(一)
Minimum editing distance (linear DP writing method)
Unzip log. GZ file
The browser enters the URL address, and what happens to the page rendering
mysql学习1:安装mysql
Operators are also important if you want to learn the C language well
Nmcli team bridge basic configuration
How histrix works
【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
Neo4j:入门基础(一)之安装与使用
word文本框换页
Sword finger offer 04 Find in 2D array
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
uniapp下拉弹层选择框效果demo(整理)
ssh服务器配置文件sshd_config 及操作
全志A13折腾备忘
自定义多线程基类threading.Event








