加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

函数指针typedef的前向声明

发布时间:2020-12-16 03:38:53 所属栏目:百科 来源:网络整理
导读:我遇到了一个特殊的问题.最好只是向您展示我正在尝试做什么,然后解释它. typedef void functionPointerType ( struct_A * sA );typedef struct{ functionPointerType ** functionPointerTable;}struct_A; 基本上,我有一个结构struct_A,其中包含一个指向函数
我遇到了一个特殊的问题.最好只是向您展示我正在尝试做什么,然后解释它.
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: %dn",a->value);
}

void print_stderr(struct A* a)
{
    fprintf(stderr,"stderr: %dn",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的完整定义之前.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读