Method for defining stylish structure in C

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

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

      

What is this structure declaration? ... What is the member data for this structure?
Here is the complete header file for the 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'm still not sure what the * handler means. I couldn't find any other references to the handler in the other header file. If appropriate, Task is a kind of normal work on firmware that receives and processes a message that can be flashed from external sources (for example, a Bluetooth device is trying to connect to a CSR board).

+3


source to share


5 answers


struct TaskData

only has an element that is a function pointer and this



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

      

+2


source


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 this one element.



It is probably used by some library function to call a function that the user of that library needs to define. (These are known as callback functions and are idiomatic in C.)

+2


source


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

      

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

.

A function pointer is a pointer to a function that should have

  • Return type as void

    .
  • Three types of input parameter Task

    , MessageId

    , Message

    respectively, which are again some typedef

    s.

EDIT:

Using

as mentioned in the comment below, for the variable the TaskData task;

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

      

+1


source


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

      

The only member of this structure:

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

      

ie a function pointer with a name handler

that can point to a function that returns void

and takes type arguments Task

, MessageId

andMessage

This can be addressed, 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;
}

      

+1


source


Anything inside a structure is a member. handler

is a pointer to a function that takes three type arguments Task, MessageId, Message

and returns void

. It is a member of the structure.

The main use of function pointers in a struct is to get an object oriented polymorphism function (virtual function) in C.

+1


source







All Articles