Function Pointer in C
Level 4183 keystrokes
Demonstrates function pointers to call different functions dynamically.
C code
// Function that adds two integers1int add(int a, int b) { return a + b; }// Function that subtracts two integers2int sub(int a, int b) { return a - b; }3 // Declare function pointer: returns int, takes two ints4int (*op)(int, int);// Point to add (a function name converts to its address)5op = add;// Call add through pointer: 5 + 3 = 86printf("Add: %d\n", op(5, 3));// Now point to sub7op = sub;// Call sub through pointer: 5 - 3 = 28printf("Sub: %d\n", op(5, 3));Output
Add: 8 Sub: 2
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C code and how often they appear.
(× 8)× 8,× 7;× 7"× 4:× 2{× 2}× 2\× 2%× 2