-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
39 lines (28 loc) · 1018 Bytes
/
test.c
File metadata and controls
39 lines (28 loc) · 1018 Bytes
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
36
37
38
39
#include <stdio.h>
// make a quick way to use function pointer as a type
// cdecl: declare funcPtr as pointer to function returning int
typedef int (*funcPtr)();
int printValue() {
printf("printValue()\n");
}
int printStuff() {
printf("printStuff()\n");
}
//int (*functions1[]) () = { printStuff, printValue, printStuff, printValue, printStuff, printValue };
funcPtr functions1[] = { printStuff, printValue, printStuff, printValue, printStuff, printValue };
// must pass length because there is no way to know the size of a pointer
int runFuncs(funcPtr *funcs, int len) {
printf("runFuncs() will run %d functions.\n", len);
for (int i=0; i<len; i++) {
(*funcs[i])();
}
}
int main(int argc, char** argv) {
// determine length using absolute size functions1
int length = sizeof(functions1) / sizeof(funcPtr);
printf("length of array: %d\n", length);
// make pointer to this array of function pointers
funcPtr *functions = functions1;
// call
runFuncs(functions, length);
}