Syncronizing indices of function pointer table to table contents

Posted by Thomas Matthews on Stack Overflow See other posts from Stack Overflow or by Thomas Matthews
Published on 2010-05-12T17:53:40Z Indexed on 2010/05/12 21:14 UTC
Read the original article Hit count: 199

In the embedded system I'm working on, we are using a table of function pointers to support proprietary Dynamic Libraries.

We have a header file that uses named constants (#define) for the function pointer indices. These values are used in calculating the location in the table of the function's address.

Example:
*(export_table.c)*

// Assume each function in the table has an associated declaration
typedef void (*Function_Ptr)(void);

Function_Ptr    Export_Function_Table[] =
{
  0,
  Print,
  Read,
  Write,
  Process,
};

Here is the header file:
*export_table.h*

#define ID_PRINT_FUNCTION 1
#define ID_READ_FUNCTION  2
#define ID_WRITE_FUNCTION 3
#define ID_PROCESS_FUNCTION 4

I'm looking for a scheme to define the named constants in terms of their location in the array so that when the order of the functions changes, the constants will also change.
(Also, I would like the compiler or preprocessor to calculate the indices to avoid human mistakes like typeo's.)

© Stack Overflow or respective owner

Related posts about c

    Related posts about embedded