带有函数指针的结构 [英] Struct with pointer to a function

查看:85
本文介绍了带有函数指针的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C结构中,我定义了一个函数指针,如下所示:

In a C struct I have defined a function pointer as follows:

typedef struct _sequence_t
{
  const int seq[3];
  typedef void (* callbackPtr)();
} sequence_t;

我想使用以下方法全局初始化该类型的var:

I want to initialize a var of that type globally with:

sequence_t sequences[] = {
  { { 0, 1, 2 }, toggleArmament },
};

而且我不断收到错误消息,告诉我初始化器太多.如何解决?

And I keep getting error telling me that there are too many initializers. How to work it out?

推荐答案

typedef用于声明类型的别名.由于您在此处拥有实际成员,因此请删除内部的typedef.

typedef is used to declare an alias to a type. Since you have an actual member here, remove the inner typedef.

typedef struct _sequence_t
{
  const int seq[3];
  void (* callbackPtr)();
} sequence_t;

这篇关于带有函数指针的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆