http://kerneltrap.org/node/8412 라는 곳에서 콜백 함수가 뭐냐는 질문글이 올라왔다.
나도 평소 괭장히 궁금했던 부분이라서(win32api 배울때..; 콜백함수 어쩌고.. 이런말 나올때마다 '저게 뭔소리냐' 하면서도, '가만히 있으면 중간이라도 간다'라는 옛 성인의 말씀에 따라 조용히 있었다. 사실 따로 공부하기도 싫었다...;;)
글을 열어봤더니 답변이 너무 명쾌해서 뭐라 더 할말이 없는 멋진 리플을 보았다.
그래서 옮겨놓는다.
Q: Submitted by bheema_k2007 on June 20, 2007 - 8:09am.
hi all,
what is "callback function"?.can anyone explain me please..
with example..
regards
bheem
A: It's when you tell a function to call another function (specified by you) at some point.
아주 간단히 말하자면, 인자로 함수 포인터를 넘겨서, 해당 함수에서 함수 포인터를 call 해버리는 방법을 의미한다.
콜백이라는 '뜻을 알면 무지 쉽게 이해되지만, 모르면 끝도없는' 단어를 사용해서 나를 힘들게 했다니..T^T
나도 평소 괭장히 궁금했던 부분이라서(win32api 배울때..; 콜백함수 어쩌고.. 이런말 나올때마다 '저게 뭔소리냐' 하면서도, '가만히 있으면 중간이라도 간다'라는 옛 성인의 말씀에 따라 조용히 있었다. 사실 따로 공부하기도 싫었다...;;)
글을 열어봤더니 답변이 너무 명쾌해서 뭐라 더 할말이 없는 멋진 리플을 보았다.
그래서 옮겨놓는다.
Q: Submitted by bheema_k2007 on June 20, 2007 - 8:09am.
hi all,
what is "callback function"?.can anyone explain me please..
with example..
regards
bheem
A: It's when you tell a function to call another function (specified by you) at some point.
#include <stdio.h>
static void count_to_five(int (*print_cb)(const char *))
{
char str[4];
int i;
for (i = 0; i < 5; i++) {
snprintf(str, sizeof(str), "%d", i);
(void)print_cb(str);
}
}
static int custom_puts(const char *str)
{
printf("foo: %s\n", str);
return 0;
}
int main(void)
{
count_to_five(puts); /* use libc puts() function to print numbers */
count_to_five(custom_puts); /* use our custom_puts() function to print numbers */
return 0;
}
static void count_to_five(int (*print_cb)(const char *))
{
char str[4];
int i;
for (i = 0; i < 5; i++) {
snprintf(str, sizeof(str), "%d", i);
(void)print_cb(str);
}
}
static int custom_puts(const char *str)
{
printf("foo: %s\n", str);
return 0;
}
int main(void)
{
count_to_five(puts); /* use libc puts() function to print numbers */
count_to_five(custom_puts); /* use our custom_puts() function to print numbers */
return 0;
}
아주 간단히 말하자면, 인자로 함수 포인터를 넘겨서, 해당 함수에서 함수 포인터를 call 해버리는 방법을 의미한다.
콜백이라는 '뜻을 알면 무지 쉽게 이해되지만, 모르면 끝도없는' 단어를 사용해서 나를 힘들게 했다니..T^T


덧글