Created
June 16, 2016 14:28
-
-
Save IS-BrianLian/00c843489d517ef830a6f7b967f32755 to your computer and use it in GitHub Desktop.
function_pointer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef int (*g)(int, int); | |
//function宣告 | |
int doAdd(int, int); | |
int doMinus(int, int); | |
g table[] = { | |
doAdd, | |
doMinus | |
}; | |
int main(void) { | |
//宣告 function pointer | |
//注意所設定的參數數量與型態 | |
g my_func_ptr; | |
//function pointer 指向 doAdd | |
// my_func_ptr = doAdd; | |
my_func_ptr = table[0]; | |
printf("function pointer 指向 doAdd => %d\n", (*my_func_ptr)(5, 3)); //結果:8 | |
//function pointer 指向 doMinus | |
my_func_ptr = table[1]; | |
printf("function pointer 指向 doMinus => %d\n", table[1](5, 3)); //結果:2 | |
return 0; | |
} //end main | |
int doAdd(int a, int b) { | |
return a + b; | |
} //end doAdd | |
int doMinus(int a, int b) { | |
return a - b; | |
} //end doMinus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment