42 พอยร์เตอร์เรียกฟังก์ชั่น ( pointer to function )


  • void (*ptr)(void); // คือ pointer to function.
  • void *ptr(void); // คือการประกาศฟังก์ชั่น คืนค่า pointer of void.
  • การประกาศ pointer to function ต้องใส่วงเล็บรอบชื่อกับ * ทุกครั้ง เพราะ ( ) มีศักดิ์เครื่องหมายสูงกว่า *
  • http://en.wikipedia.org/wiki/Callback_(computer_programming)

#include <stdio.h>
void greeting();
int sum(int a, int b);
int main()
{
greeting();
void (*ptr)(void) = greeting;
printf("%d , %d\n",greeting,ptr);
ptr();
int (*pSum)(int, int) = sum;
printf("%d , %d\n",sum(2,3),pSum(3,4));
return 0;
}
void greeting()
{
printf("Hello Banyentutorial.\n");
}
int sum(int a, int b)
{
return a+b;
}


#include <stdio.h>
void greeting();
int sum(int a, int b);
int main()
{
greeting();
void (*ptr)(void) = greeting;
printf("%d , %d\n",greeting,ptr);
ptr();
int (*pSum)(int, int) = sum;
printf("%d , %d\n",sum(2,3),pSum(3,4));
return 0;
}
void greeting()
{
printf("Hello Banyentutorial.\n");
}
int sum(int a, int b)
{
return a+b;
}