我遇到了一个特殊的问题.最好只是向您展示我正在尝试做什么,然后解释它.
typedef void functionPointerType ( struct_A * sA ); typedef struct { functionPointerType ** functionPointerTable; }struct_A;
基本上,我有一个结构struct_A,其中包含一个指向函数指针表的指针,这些函数指针的参数类型为struct_A.但我不确定如何进行编译,因为我不确定如何或是否可以转发声明这一点.
谁知道如何实现这一目标?
编辑:代码中的小修复
解决方法
按照你的建议转发声明:
/* Forward declare struct A. */ struct A; /* Typedef for function pointer. */ typedef void (*func_t)(struct A*); /* Fully define struct A. */ struct A { func_t functionPointerTable[10]; };
例如:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct A; typedef void (*func_t)(struct A*); struct A { func_t functionPointerTable[10]; int value; }; void print_stdout(struct A* a) { printf("stdout: %d\n",a->value); } void print_stderr(struct A* a) { fprintf(stderr,"stderr: %d\n",a->value); } int main() { struct A myA = { {print_stdout,print_stderr},4 }; myA.functionPointerTable[0](&myA); myA.functionPointerTable[1](&myA); return 0; }
输出:
stdout: 4 stderr: 4
参见在线演示http://ideone.com/PX880w.
正如其他人已经提到的那样,可以添加:
typedef struct A struct_A;
如果最好省略struct关键字,则在函数指针typedef和struct A的完整定义之前.
原文地址:https://www.jb51.cc/c/110799.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。