user13267
user13267

Reputation: 7193

weird structure declaration method in C

This is from the header file for the library functions of CSR8670 Bluetooth chip

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. 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).

Upvotes: 3

Views: 229

Answers (5)

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

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

This is a function pointer, which is the only member of the structure struct TaskData.

The function pointer is a pointer to a function which should have

  • Return type as void.
  • Three input parameter of type Task, MessageId, Message, respectively, which are again some typedefs.

EDIT:

Usage

as Mentioned in the comment below, for a variable 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

Upvotes: 1

haccks
haccks

Reputation: 106012

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. 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.

Upvotes: 1

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

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

The only member of this structure is:

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

i.e a function pointer named handler which can point to a function which returns void & takes arguments of type Task, MessageId, and Message

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;
}

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234705

handler is a pointer to a function that returns void and has parameters with types Task, MessageId, and Message in that order.

TaskData is a structure containing that one member.

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.)

Upvotes: 2

Gopi
Gopi

Reputation: 19864

The struct TaskData has only member which is a function pointer and it is

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

Upvotes: 1

Related Questions