Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int addInt(int n, int m){
- return n + m;
- }
- int timeInt(int n, int m){
- return n * m;
- }
- int main(){
- /************** function pointer *****************/
- int sum = 0;
- // declare
- int (*functionPtr)(int,int);
- // read values
- // 1. assign memory address of a function to the pointer
- functionPtr = addInt; // 或這樣寫: functionPtr = &addInt;
- // compute
- sum = functionPtr(2,3); // 2 + 3 = 5 // 或這樣寫: sum = (*functionPtr)(2,3);
- // show result
- printf("%d\n",sum);
- // read values
- // 2. assign memory address of another function
- functionPtr = timeInt;
- // compute
- sum = functionPtr(2,3); // 2 * 3 = 6
- // show result
- printf("%d\n",sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement