Please help in array of struct.

This is a discussion on Please help in array of struct. within the C Programming forums, part of the General Programming Boards category; Hi, I am reading C programs. Can any body explain me followng structure struct x_driver { int (*Initialize) (int port_offset); ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Question Please help in array of struct.

    Hi,


    I am reading C programs. Can any body explain me
    followng structure
    struct x_driver
    {
    int (*Initialize) (int port_offset);
    int (*Shutdown) ();
    int (*GetNumPorts) ();
    };

    struct x_driver driver[] = {
    {
    &tpInit,
    &tpShutdown,
    &tpGetNumPorts,
    },
    {
    #ifdef EPI
    &epiInit,
    &epiShutdown,
    &piIGetNumPorts,
    #endif
    }
    };

    driver[0].Initialize(0);
    driver[0].GetNumPorts(0);

    I understand driver[0].Initialize(0); initialise the drivers by calling some function. jist I wanted to know what is that function

    Can anybody please tellme what is meaningof
    driver[0].Initialize(0); driver[0].GetNumPorts(0);

    I appreciat any help

    Thanks
    Babji

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I understand driver[0].Initialize(0); initialise the drivers by
    >calling some function. jist I wanted to know what is that function
    driver[0].Initialize(0) calls the Initialize function pointer of the 0th element of the driver array, which in this case is the function tpInit.

    >Can anybody please tellme what is meaningof driver[0].Initialize(0); driver[0].GetNumPorts(0);
    Function pointers are fun, no? Initialize and GetNumPorts are function pointers, when driver[0] is declared, it is also initialized with the address of function definitions that the function pointers are assigned to. So driver[0].Initialize(0); will call the tpInit function and driver[0].GetNumPorts(0); will call the tpGetNumPorts function.

    Each inner brace of the array initialization list assigns the address of certain functions to the function pointers of each element, starting with zero. So the first three addresses (tpInit, tpShutDown, and tpGetNumPorts) are assigned to driver[0] and the next three are assigned to driver[1] if EPI is defined.
    Code:
    struct x_driver 
    { 
      int (*Initialize) (int port_offset); 
      int (*Shutdown) (); 
      int (*GetNumPorts) (); 
    }; 
    
    struct x_driver driver[] = { 
      { &tpInit, &tpShutdown, &tpGetNumPorts, }, 
      { 
        #ifdef EPI 
        &epiInit, &epiShutdown, &piIGetNumPorts, 
        #endif 
      } 
    }; 
    
    driver[0].Initialize(0); 
    driver[0].GetNumPorts(0);
    p.s. This is C code and should be in the C board, not the C# board.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    ya`

    your right prelude.. functions are fun.. no?
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21