Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help Needed on C Programming (http://www.go4expert.com/forums/help-c-programming-t27366/)

clc 14Dec2011 12:13

Help Needed on C Programming
 
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

xpi0t0s 16Dec2011 01:13

Re: Help Needed on C Programming
 
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".


All times are GMT +5.5. The time now is 23:35.