Help Needed on C Programming

clc's Avatar, Join Date: Dec 2011
clc
Newbie Member
Hi,
Does anyone know what does "req.dataRetHandler = SetFOO" means in the following code?

Code:
typedef struct 
{
 
  unsigned char command;                           
  unsigned int (*dataRetHandler)(unsigned char, unsigned char);
 
} queue_t;
 
unsigned char PushInOQ(unsigned char channel, queue_t *pReq )
{
     printf("Command = %02X\n",pReq->command);
     return 0;
}
 
unsigned int SetFOO(unsigned char channel, unsigned char uData)
{
    queue_t req;
    unsigned char result = 0;
    req.command = 0x04;
    result = PushInOQ( 0, &req);
    return (0);
}
void main(void)
{
    queue_t req;
    unsigned char result = 0;
    req.command = 0x01;
    req.dataRetHandler = SetFOO;
    result = PushInOQ( 0, &req);
}

Thanks in advance.

Regards
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
req.dataRetHandler is a function pointer, so it sets req.dataRetHandler to the address of the function SetFOO(). So you can call req.dataRetHandler as if it were a function (using function pointer syntax, of course) and that way you don't have to have a big if/else if ladder in the code deciding which of multiple functions to call.

But the code in its current form doesn't actually call req.dataRetHandler, so in effect the answer to your question is "nothing significant".