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.)
See this answer for a way to coerce the preprocessor in doing it for you.