특정 변수에 대한 메모리 주소를 담을 수 있는 변수를 포인터 변수,
특정 함수에 대한 메모리 주소를 담을 수 있는 변수를 함수 포인터.
프로그램 코드가 간결해집니다.
중복되는 코드를 줄일 수 있습니다.
상황에 따른 함수를 호출할 수 있습니다.
int (*FuncPtr)(int, int)
int add(int first, int second)
1. FuncPtr = add
2. FuncPtr = &add
#include <stdio.h> #include <iostream> typedef int(*calcFuncPtr)(int, int); int plus(int first, int second) { return first + second; } int minus(int first, int second) { return first - second; } int calculator(int first, int second, calcFuncPtr func) { return func(first, second); } int main(int argc, char** argv) { calcFuncPtr calc = NULL; int a = 0, b = 0; char op = 0; int result = 0; scanf("%d %c %d", &a, &op, &b); switch (op) { case '+' : calc = plus; break; case '-' : calc = minus; break; } result = calculator(a, b, calc); printf("result : %d\n", result); system("pause"); return 0; } |
출처: http://norux.me/8