Hareesh Nagarajan (hnagaraj AT cs uic edu)
Mon Nov 21 00:58:19 CST 2005

This document may never be updated

#include <stdio.h>
struct X {
	int x;
	void (*fp)(struct X *);
};

void func1(struct X *xobj) { printf("%s, %d\n", __FUNCTION__, xobj->x); }
void func2(struct X *xobj) { printf("%s, %d\n", __FUNCTION__, xobj->x); }

main() {
	struct X obj;
	obj.x  = 10;
	obj.fp = func1;
	obj.fp(&obj);
	obj.x  = 20;
	obj.fp = func2;
	obj.fp(&obj);
}

Output

$ ./a.out
func1, 10
func2, 20

Cool eh?

 

copyright

https://www2.cs.uic.edu/~hnagaraj/articles/function-pointers/

 

相关文章:

  • 2021-10-13
  • 2021-10-22
  • 2021-11-03
  • 2021-07-03
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2022-02-22
  • 2021-12-22
  • 2021-08-07
相关资源
相似解决方案