-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path024_FunctionPointers_JumpTables.cpp
More file actions
57 lines (45 loc) · 1.38 KB
/
024_FunctionPointers_JumpTables.cpp
File metadata and controls
57 lines (45 loc) · 1.38 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// a simple jump table
#include <cstdio>
using namespace std;
const char * prompt();
int jump( const char * );
void fa() { puts("this is fa()"); }
void fb() { puts("this is fb()"); }
void fc() { puts("this is fc()"); }
void fd() { puts("this is fd()"); }
void fe() { puts("this is fe()"); }
void (*funcs[])() = { fa, fb, fc, fd, fe };
int main() {
while(jump(prompt())) ;
puts("\nDone.");
return 0;
}
const char * prompt() {
puts("Choose an option:");
puts("1. Function fa()");
puts("2. Function fb()");
puts("3. Function fc()");
puts("4. Function fd()");
puts("5. Function fe()");
puts("Q. Quit.");
printf(">> ");
fflush(stdout); // flush after prompt
const int buffsz = 16; // constant for buffer size
static char response[buffsz]; // static storage for response buffer
fgets(response, buffsz, stdin); // get response from console
return response;
}
int jump( const char * rs ) {
char code = rs[0];
if(code == 'q' || code == 'Q') return 0;
// get the length of the funcs array
int func_length = sizeof(funcs) / sizeof(funcs[0]);
int i = (int) code - '0'; // convert ASCII numeral to int
if( i < 1 || i > func_length ) {
puts("invalid choice");
return 1;
} else {
funcs[i - 1](); // array is zero-based
return 1;
}
}