I'm trying to understand a firmware written in C that drives a chip for ultrawideband connections.
The firmware does heavy use of typedef
and pointers. I've understood most of the idea behind the firmware but there's a typedef void
function I can't understand.
Basically, the firmware creates a structure to hold the device data with
typedef struct
{
//some data
dwt_cb_t cbTxDone; // Callback for TX confirmation event
//some other data
} dwt_local_data_t ;
I've understood that the structure is named dwt_local_data_t and contains some variables including this strange dwt_cb_t type.
On the .h file dwt_cb_t is named as
// Call-back type for all events
typedef void (*dwt_cb_t)(const dwt_cb_data_t *);
where dwt_cb_data_t is another structure in the form of
typedef struct
{
uint32 status; //initial value of register as ISR is entered
uint16 datalength; //length of frame
uint8 fctrl[2]; //frame control bytes
uint8 rx_flags; //RX frame flags, see above
} dwt_cb_data_t;
Now, I'm trying to understand the meaning of typedef void (*dwt_cb_t)(const dwt_cb_data_t *);
From what I've understood, typedef void
is a pointer-to-function type. It defines a variable dwt_cb_t that points to a function receiving as an input a constant struct dwt_cb_data_t
I don't know if my reasoning is correct because I can't understand why there's a *
spaced at the end of dwt_cb_data_t. Does it mean that the input of the function is the pointer of the structure? In this case, why don't write typedef void (*dwt_cb_t)(const *dwt_cb_data_t);
instead?
const *dwt_cb_data_t
is not a valid declaration for a parameter? Why do you think this should be equivalent toconst dwt_cb_data_t*
? – Goldsteinconst *dwt_cb_data_t
is legal syntax. A pointer toT
is always writtenT*
not*T
. – Jehuconst dwt_cb_data_t
and not onlyconst
. It makes sense now to put*
at the very end – Consultation