简体   繁体   中英

Accessing an array of typedef struct

Hi guys I am working on Microchip based PIC32MX example for Printer host wherein i came accross following code wherin in the program to find out client driver to be used the following if loop is used.I am unable to understand the execution of the loop ,

I wish to know how the if loop returns 1 or 0 , i got stuck at type def for usb_client_init function pointer , Also i do not understand the working of the CLIENT_DRIVER_TABLE usbClientDrvTable[] array , how it decides which function to use . The initialize .operator should call the pointer function *USB_CLIENT_INIT but their is no definition for USB_CLIENT_INIT it is only being declared .

    typedef BOOL (*USB_CLIENT_INIT)  (BYTE, DWORD ,BYTE);

        typedef struct _CLIENT_DRIVER_TABLE {
            USB_CLIENT_INIT Initialize;     // Initialization routine  

          } CLIENT_DRIVER_TABLE;

        CLIENT_DRIVER_TABLE usbClientDrvTable[] = {   
         {                                     
            USBHostPrinterInitialize,    // It is a function 
            USBHostPrinterEventHandler,  // It is a function 
            0
         },

         { USBHostPrinterInitialize,    // It is a function 
            USBHostPrinterEventHandler,  // It is a function 
           1
         }   
     };


void main()
{


while(1)
{
char temp=0;
  if( !usbClientDrvTable[temp].Initialize(a,b,c))
  {
  //do something 

  }

}

    }

It looks like you removed two lines from the struct _CLIENT_DRIVER_TABLE . There should be EventHandler and flags as well.

The first typedef declares USB_CLIENT_INIT to be a pointer to a function that takes 3 variables and returns a bool. (You are missing a similar declaration for an event handler.)

The next typedef declares one line of the table. It should have three parts, but yours only has the Initialize part left, which is a function pointer of type USB_CLIENT_INIT .

Next an array of two of these entries is declared. The { and } indicate that these two entries should have three parts, not just one.

Finally, you use this array.

usbClientDrvTable[0] is the first element of the array.

usbClientDrvTable[0].Initialize is the function pointer in the first struct. (It has been defined to be USBHostPrinterInitialize .)

usbClientDrvTable[0].Initialize(a,b,c) is the boolean result of applying that function to the values a,b, and c. This is the same as USBHostPrinterInitialize(a,b,c) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM