C++ function Pointers and callbacks use parsing

  • 2020-06-12 10:03:40
  • OfStack

A function pointer

A function pointer is a pointer to a function.

Usually a pointer variable refers to an integer, character, or array variable, while a function pointer refers to a function.

A function pointer can be used like a function 1 to call a function and pass arguments.

Declaration of function pointer variable:


typedef int (*fun_ptr)(int,int); //  The statement 1 A function pointer variable that takes the same argument and returns the same value 

The instance

The following example declares the function pointer variable p, which points to the function max:


#include <stdio.h> 
int max(int x, int y){ 
return x > y ? x : y;
} 
int main(void){ 
/* p  It's a function pointer  */ 
int (* p)(int, int) = & max; // & Can be omitted  
int a, b, c, d; 
printf(" Please enter the 3 A digital :"); 
scanf("%d %d %d", & a, & b, & c);  /*  The equivalent of calling a function directly, d = max(max(a, b), c) */ 
d = p(p(a, b), c);  
printf(" The largest number is : %d\n", d);  
return 0;
}
#include <stdio.h> 
int max(int x, int y){ 
return x > y ? x : y;
} 
int main(void){ 
/* p  It's a function pointer  */ 
int (* p)(int, int) = & max; // & Can be omitted  
int a, b, c, d; 
printf(" Please enter the 3 A digital :"); 
scanf("%d %d %d", & a, & b, & c);  /*  The equivalent of calling a function directly, d = max(max(a, b), c) */ 
d = p(p(a, b), c);  
printf(" The largest number is : %d\n", d);  
return 0;
}

Compile and execute, and the output is as follows:

[

Please enter 3 Numbers :1, 2, 3
The largest number is: 3

]

The callback function

A function pointer as an argument to a function

A function pointer variable can be used as an argument to a function. A callback function is a function called by a function pointer.

Simply put: Callback functions are functions that are called when someone else's function executes.

[

You go to a store to buy something, just what you want is not in stock, so you leave your phone number in the store, a few days later the store is in stock, the clerk calls your phone number, and then you get the phone number after you go to the store to pick up the goods. So in this case, your phone number is called a callback function, you leave your phone number with the clerk and you call the register callback function, and when the store has a stock it's called an event that triggers the callback association, and when the clerk calls you it's called a callback function, and when you go to the store to pick up the item it's called a response callback event.

]

The instance

The populate_array function in the instance defines three arguments, the third of which is a pointer to the function that sets the value of the array.

In the example, we define the callback function getNextRandomValue, which returns a random value that is passed to the populate_array function as a function pointer.

populate_array calls the callback function 10 times and assigns its return value to the array.


#include <stdlib.h> 
 #include <stdio.h> 
//  The callback function void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)){ 
for (size_t i=0; i<arraySize; i++)
  array[i] = getNextValue();
} 
//  Get random value 
int getNextRandomValue(void){ 
return rand();
} 
int main(void){ 
int myarray[10]; 
populate_array(myarray, 10, getNextRandomValue); 
for(int i = 0; i < 10; i++) {  
 printf("%d ", myarray[i]);
 } 
printf("\n"); 
return 0;
}

Compile and execute, and the output is as follows:

[

1680728247524916226500739849436581144108930470211272101027544145785087814587779232007237709

]

Related articles: