Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Function Pointers and Callback Functions

Function pointers

A function pointer is a pointer variable that points to a function.

Usually, when we talk about pointer variables we mean pointers to integers, characters, arrays, and so on, whereas a function pointer points to a function.

A function pointer can be used to call a function or pass arguments, just like a regular function.

Declaration of a function pointer type:



typedef int (*fun_ptr)(int,int); // 声明一个指向同样参数、返回值的函数指针类型

Example

The following example declares a function pointer variable p that points to the function max:

Example

#include

<

stdio.h

int

max

(

int

x

,

int

y

)

{

return

x

y

?

x

:

y

;

}

int

main

(

void

)

{

/*

p is a function pointer

*/

int

(

p

)

(

int

,

int

)

= &

max

;

// The & can be omitted

int

a

,

b

,

c

,

d

;

printf

(

"

Please enter three numbers:

"

)

;

scanf

(

"

%d %d %d

"

, &

a

, &

b

, &

c

)

;

/*

Equivalent to calling the function directly: d = max(max(a, b), c)

*/

d

=

p

(

p

(

a

,

b

)

,

c

)

;

printf

(

"

The largest number is: %d

\

n

"

,

d

)

;

return

0

;

}

After compiling and executing, the output is as follows:



请输入三个数字:1 2 3

最大的数字是: 3


Callback Function

Function pointers as arguments to a function

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

Simply put, a callback function is a function you implement that gets called by someone else's function.

Here is an explanation from Zhihu author Chang Xiling:

You go to a store to buy something, but the item you want is out of stock, so you leave your phone number with the clerk. A few days later the store gets the item, the clerk calls you, and after receiving the call you go to the store to pick it up. In this example, your phone number is the callback function, leaving your number with the clerk is registering the callback function, the store getting the item is triggering the event associated with the callback, the clerk calling you is invoking the callback function, and your going to the store to pick up the item is responding to the callback event.

Example

In the example, populate_array() the function defines three parameters, the third of which is a function pointer used to set the values of the array.

In the example we define the callback function getNextRandomValue(), which returns a random value and is passed as a function pointer to populate_array() function in Vue 3.

populate_array() will call 10 times, and assign the return value of the callback function to the array.

Example

#include

<

stdlib.h

#include

<

stdio.h

void

populate_array

(

int

array

,

size_t

arraySize

,

int

(

getNextValue

)

(

void

)

)

{

for

(

size_t

i

=

0

;

i

<

arraySize

;

i

++

)

array

[

i

]

=

getNextValue

(

)

;

}

// Get a random value

int

getNextRandomValue

(

void

)

{

return

rand

(

)

;

}

int

main

(

void

)

{

int

myarray

[

10

]

;

/*

getNextRandomValue cannot have parentheses; otherwise it will not compile, because adding parentheses means passing an int rather than a function pointer as this argument.

*/

populate_array

(

myarray

,

10

,

getNextRandomValue

)

;

for

(

int

i

=

0

;

i

<

10

;

i

++

)

{

printf

(

"

%d

"

,

myarray

[

i

]

)

;

}

printf

(

"

\

n

"

)

;

return

0

;

}

After compiling and executing, the output is as follows:



16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709