本人略懶,不喜長篇大論的寫些語法特性什麼的,前兩天無事隨手打開一PDF,看到關於函數指針的用法以前沒有這麼用過,遂寫一小例試試,其實感覺函數指針與面向對象有異曲同工之妙(如果理解有誤,,還請大家多多包含並指正^_^!!!)
- #include <stdio.h>
- typedef struct {
- void (*print_it)();
- int (*max_get)(int x, int y);
- }func_pointer;
- void print_err()
- {
- printf("This is for test!\n");
- }
- void print_end()
- {
- printf("****** END ***********\n");
- }
- int max_two(int x, int y)
- {
- return( x > y ? x : y);
- }
- int main(int argc, char **argv)
- {
- func_pointer test;
- test.print_it = print_err;
- test.max_get = max_two;
- test.print_it();
- printf("%d\n", test.max_get(9,78));
- test.print_it = print_end;
- test.print_it();
- return 0;
- }