简体   繁体   English

C中奇怪的结构声明方法

[英]weird structure declaration method in C

This is from the header file for the library functions of CSR8670 Bluetooth chip 这是来自CSR8670蓝牙芯片库函数的头文件

typedef struct TaskData { void (*handler)(Task, MessageId, Message); } TaskData;  

What kind of structure declaration is this? 这是什么样的结构声明? What are the member data for this structure? 这个结构的成员数据是什么?
Here is the full header file for context: 这是上下文的完整头文件:

/* This file was automatically generated from syscalls.in 17.2 */

#ifndef __MESSAGE__H

#define __MESSAGE__H

#include <csrtypes.h>
/*! @file message_.h @brief Message types */
/*!
Message identifier type.
*/
typedef uint16 MessageId;
/*!
Message delay type.
*/
typedef uint32 Delay;
/*!
Message type.
*/
typedef const void *Message;
/*!
Task type.
*/
typedef struct TaskData *Task;
/*!
TaskData type.
*/
typedef struct TaskData { void (*handler)(Task, MessageId, Message); } TaskData;

#endif  

I am still not sure what *handler means. 我仍然不确定*处理程序的含义。 I have not been able to find any other references to handler in the other header file. 我无法在其他头文件中找到对handler的任何其他引用。 If it's relevant, Task represents a sort of routine running on the firmware that accepts and processes the message that the firmware may receive from external sources (for example, a bluetooth device trying to connect to the CSR board). 如果相关,则Task表示在固件上运行的一种例程,该例程接受并处理固件可能从外部源接收的消息(例如,尝试连接到CSR板的蓝牙设备)。

struct TaskData只有一个成员,它是一个函数指针

void (*handler)(Task, MessageId, Message);

handler is a pointer to a function that returns void and has parameters with types Task , MessageId , and Message in that order. handler是一个指向函数的指针,该函数返回void并且具有类型为TaskMessageIdMessage参数。

TaskData is a structure containing that one member. TaskData是包含该成员的结构。

It's probably used by some library function to call a function that the user of that library has to define. 它可能被某些库函数用来调用该库的用户必须定义的函数。 (These are known as callback functions and are idiomatic in C.) (这些被称为回调函数,在C中是惯用的。)

void (*handler)(Task, MessageId, Message);

This is a function pointer, which is the only member of the structure struct TaskData . 这是一个函数指针,它是结构struct TaskData的唯一成员。

The function pointer is a pointer to a function which should have 函数指针是指向应具有的函数的指针

  • Return type as void . 返回类型为void
  • Three input parameter of type Task , MessageId , Message , respectively, which are again some typedef s. 三个输入参数分别是TaskMessageIdMessage ,它们也是一些typedef

EDIT: 编辑:

Usage 用法

as Mentioned in the comment below, for a variable TaskData task; 正如下面的评论中提到的,对于变量TaskData task; the access should be [in pseudocode] 访问应该是[伪代码]

// void somefunc(Task t, MessageId mid, Message m) is the function
task.handler = somefunc;  

and

Task p;
MessageId q;
Message r;

task.handler(p,q,r);   
//function somefunc() will be called with argument p, q,and r
typedef struct TaskData { void (*handler)(Task, MessageId, Message); } TaskData;

The only member of this structure is: 这个结构的唯一成员是:

void (*handler)(Task, MessageId, Message);

ie a function pointer named handler which can point to a function which returns void & takes arguments of type Task , MessageId , and Message 即一个名为handler的函数指针,它可以指向一个返回void的函数,并获取TaskMessageIdMessage类型的参数

This can be accessed for example like this: 这可以访问,例如:

typedef struct
{
  void (*hand)(int a);
} str;

void func(int a)
{
  printf("Value of a = %d\n", a);
}

int main ()
{
  str var;

  var.hand = func;
  var.hand(25);

  return 0;
}

Any thing inside a structure is its member. 结构中的任何东西都是它的成员。 handler is a function pointer to a function which accepts three arguments of types Task, MessageId, Message and returns void . handler是一个指向handler的函数指针,该函数接受类型为Task, MessageId, Message三个参数并返回void This is a member of structure. 这是结构的一员。

The main use of function pointers in structure is used to get the object oriented feature of Polymorphism (virtual function) in C. 结构中函数指针的主要用途是用C来获取多态性(虚函数)的面向对象特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM