在C中,指向函数作为结构成员的指针是如何有用的?

gr8qqesn  于 2023-11-17  发布在  其他
关注(0)|答案(6)|浏览(149)

我对C编程并不陌生。但我不明白为什么在C中将指向函数的指针作为结构成员是有用的。示例:

  1. // Fist Way: To keep pointer to function in struct
  2. struct newtype{
  3. int a;
  4. char c;
  5. int (*f)(struct newtype*);
  6. } var;
  7. int fun(struct newtype* v){
  8. return v->a;
  9. }
  10. // Second way: Simple
  11. struct newtype2{
  12. int a;
  13. char c;
  14. } var2;
  15. int fun2(struct newtype2* v){
  16. return v->a;
  17. }
  18. int main(){
  19. // Fist: Require two steps
  20. var.f=fun;
  21. var.f(&var);
  22. //Second : simple to call
  23. fun2(&var2);
  24. }

字符串
程序员是用它来给予面向对象(OO)的形式给他们的C代码并提供抽象对象,还是只是让代码看起来像技术性的?
我认为在上面的代码中,第二种方法更温和,也很简单。在第一种方法中,我们仍然需要传递&var,即使fun()是结构体的成员。
如果在结构定义中保留函数指针是好的,请解释原因。

uurity8g

uurity8g1#

提供指向结构上的函数的指针可以使您动态选择要对结构执行的函数。

  1. struct newtype{
  2. int a;
  3. int b;
  4. char c;
  5. int (*f)(struct newtype*);
  6. } var;
  7. int fun1(struct newtype* v){
  8. return v->a;
  9. }
  10. int fun2(struct newtype* v){
  11. return v->b;
  12. }
  13. void usevar(struct newtype* v) {
  14. // at this step, you have no idea of which function will be called
  15. var.f(&var);
  16. }
  17. int main(){
  18. if (/* some test to define what function you want)*/)
  19. var.f=fun1;
  20. else
  21. var.f=fun2;
  22. usevar(var);
  23. }

字符串
这使您能够拥有单个调用接口,但根据测试是否有效而调用两个不同的函数。

展开查看全部
thigvfpy

thigvfpy2#

如果你想做一些“基于对象”的编程,它很有用。
如果你曾经看过雷神之锤3引擎的源代码,你可以清楚地看到,大多数“实体”都有定义它们的属性,以及它们所做的工作[这是函数指针]。
分离属性和函数(通过C中的函数指针)定义了“结构”对象的属性和它们可以执行的操作。
举例来说:

  1. struct _man{
  2. char name[];
  3. int age;
  4. void (*speak)(char *text);
  5. void (*eat)(Food *foodptr);
  6. void (*sleep)(int hours);
  7. /*etc*/
  8. };
  9. void grijesh_speak(char *text)
  10. {
  11. //speak;
  12. }
  13. void grijesh_eat(Food *food)
  14. {
  15. //eat
  16. }
  17. void grijesh_sleep(int hours)
  18. {
  19. //sleep
  20. }
  21. void init_struct(struct _man *man)
  22. {
  23. if(man == NULL){ man = malloc(sizeof(struct _man));}
  24. strcpy(*man.name,"Grijesh");
  25. man->age = 25;
  26. man->speak = grijesh_speak;
  27. man->eat = grijesh_food;
  28. man->sleep = grijesh_sleep;
  29. //etc
  30. }
  31. //so now in main.. i can tell you to either speak, or eat or sleep.
  32. int main(int argc, char *argv[])
  33. {
  34. struct _man grijesh;
  35. init_struct(&grijesh);
  36. grijesh.speak("Babble Dabble");
  37. grijesh.sleep(10);
  38. return 0;
  39. }

字符串

展开查看全部
jei2mxaa

jei2mxaa3#

我过去用它在C中实现了泛型容器。
举例来说:

  1. typedef struct generic_list_node {
  2. void *data;
  3. struct generic_list_node *next;
  4. } generic_list_node_t;
  5. typedef struct generic_list {
  6. generic_list_node_t *head;
  7. void *(*copy)(void *data);
  8. void (*delete)(void *data);
  9. int (*compare)(void *lhs, void *rhs);
  10. void (*display)(void *data, FILE *stream);
  11. } generic_list_t;

字符串
列表结构本身是数据不可知的,使用void *来表示数据项,并将所有类型感知的操作委托给上面的指针所指示的函数:

  1. int insert(generic_list_t l, void *data)
  2. {
  3. generic_list_node_t *cur = l.head;
  4. generic_list_node_t *new = malloc(sizeof *new);
  5. if (new)
  6. {
  7. new->data = l.copy(data);
  8. new->next = NULL;
  9. if (l.head == NULL)
  10. {
  11. l.head = new;
  12. }
  13. else
  14. {
  15. while (cur->next && l.compare(new->data, cur->data) > 0)
  16. cur = cur->next;
  17. new->next = cur->next;
  18. cur->next = new;
  19. printf("Successfully added ");
  20. l.display(data, stdout);
  21. printf(" to list\n");
  22. }
  23. return 1;
  24. }
  25. return 0;
  26. }


1.对于“通用”和“容器”的适当宽松的定义,

展开查看全部
wwtsj6pe

wwtsj6pe4#

这在嵌入式系统或驱动程序编写中特别有用。函数是使用函数指针调用的。
例如

  1. struct_my_filesystem.open=my_open;
  2. struct_my_filesystem.read=my_read;

字符串

zf2sa74q

zf2sa74q5#

在C语言中,有时候你需要调用一个函数,而不需要预先知道它的实现。例如,如果你正在开发一个在不同项目中使用的独立库。在这种情况下,向上下文结构添加一个函数指针并将其沿着传递给库函数是非常有意义的。然后库函数可以在运行时调用你的函数,而不必包含定义它的头文件。
它确实类似于你在做面向对象编程时所做的事情。在C中,你通常传递上下文变量,例如一个结构,将一组变量和可能对该上下文有意义的函数组合在一起。这接近于OO编程中的等价,在OO编程中,你示例化一个类并在示例上设置所需的变量。

30byixjq

30byixjq6#

这里有一个项目来帮助解释函数指针的用处。尝试创建一个基于c的库,提供继承和多态性。或者,尝试重新创建“C with Classes”。结构内的函数指针将是至关重要的。http://www.cs.rit.edu/~ats/books/ooc.pdf

相关问题