1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
void say_hi() {
printf("Hi\n");
}
void say_bye() {
printf("Bye\n");
}
// say_something(…) takes the address of a function as its sole parameter.
// The name of the parameter is 'say_fn'. 'say_fn' must be a function that
// returns void and takes no parameters.
void say_something(void(*say_fn)()) {
// ▼ ▼ └───> types of parameters that say_fn takes
// «type fn returns» «name of parameter»
// Call the function that was passed to say_something(…)
say_fn();
}
int main(int argc, char* argv[]) {
say_something( say_hi ); // say_hi is the address of the say_hi(…) function.
say_something( say_bye ); // say_bye is the address of the say_bye(…) function.
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2023 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.