07. More on memory
- Pointer to function
address of a function in memory. Can be used by '*'-ing.
E. g.:
Create a dummy calculator with 4 arithmetic operators (+, -, *, /), that cyclically reads 3 integers, A, B, and N and prints the result of operation corresponded to N (0 is '+', 1 is '-', 2 is '*' and 3 is '/'). If N is not in the diapason, the program stops
- Use function pointer array as this:
1 int (*table[])(int, int) = { add, sub, mul, div };
- Use function pointer array as this:
- Rewrite this to use characters for operators. After character input, you have to search though the massive of struct elements:
Caution: command can be only «a+b», not «a + b»
Add another operation «^» that performs integer power ab
See documentation to find that scanf is returning integer value, equal to the number of items it has actually scanned successfully. Display an error if there are less than 3 variables scanned.
H/W
- Finish all tasks
Place the solution to the sugon server under the name fpointer.c. E. g. like this:
$ scp -P -P 2131 yourprog.c your_login@sugon:fpointer.c
(note the address of sugon)