//      funcPtr.c
//      Copyright 2009 Kim Sung-tae <pchero@MyNote>
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      GNU General Public License for more details.
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

#include <stdio.h>

#define FUNC1 func1
#define FUNC2 func2

int func1(int intVal1) { return –intVal1; }
int func2(int intVal2) { return –intVal2; }

int runFunc(char* string, int inVal)
    int retVal;
    int (*funcPtr)(int retVal);
    funcPtr = string;
    return (*funcPtr)(inVal);

int main(int argc, char** argv)
    int intVal = 3;
    intVal = runFunc(FUNC2, intVal);
    printf(“After running func2 intVal : %dn”, intVal);
    intVal = runFunc(FUNC1, intVal);
    printf(“After running func1 intVal : %dn”, intVal);
    return 0;

 함수 이름을 포인터에 할당하여 사용하는 재미있는 프로그램.
 함수들을 필요한 순서대로 만들어 둔 뒤, 포인터를 이용하여 상황에 따라 적절한 함수가 호출되도록 만든다면 상태머신(State machine)프로그램 등 유용하게 사용할 수 있을 것이다.

Tags: ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.