I have a C header file (.h):
typedef uint8_t paraFunction(uint8_t paraVal, uint8_t paraNum);
paraFunction *paraCallTable[256];
And I have a C source file (.c):
paraFunction *paraCallTable[] = { fn1, fn2, ... fn255, fn256 };
However, the array ends up in the data section. I want it to be in code/flash because the function addresses (the array data) are constant. The array is used by functions in other source files.
How do I rewrite these declarations to make it all constant? And where do they need to be (.c or .h)?
I googled and tried a gazillion different ways and I obviously just don't get it.
Since you are already using the clearest (IMHO) form of function pointer typedef
, it's simply a matter of adding const
to the declaration at the right side of the *
, like you would with an object pointer you'd like to end up in ROM:
paraFunction *const paraCallTable[]
Had you used the form typedef uint8_t (*paraFunction)(uint8_t paraVal, uint8_t paraNum);
then you'd have to use the far less obvious and confusing form const paraFunction paraCallTable[]
. This is a read-only pointer to data, not read-only data or pointer to read-only data as one might think.
This is a solid argument for sticking to the good typedef
style you are already using.
We may also note that it's good practice to always declare function pointer tables as * const
, ROM or no ROM, since changing function pointers in run-time is most often undesired.