1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help Needed on C Programming

Discussion in 'C' started by clc, Dec 14, 2011.

  1. clc

    clc New Member

    Joined:
    Dec 14, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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".
     

Share This Page