What I have is an array of unsigned chars the first byte is actually the number of elements in the array. So is it possible to have the compiler calculate the size of the array using sizeof()? or is there some other way?
#define MENU(ID) menu_##ID #define MENU_CNT_P(N) ((sizeof(MENU(N))-1)/sizeof(menu_position))
This of course gives 0 for the sizeof() value because the array isn't defined. Suggestions/thoughts welcome. Stephen
This of course gives 0 for the sizeof() value because the array isn't defined
That's strange. I would expect a compilation error. Why not describe what you are trying to do? Then we would be able to suggest something...
what I often do in cases like this is that I insert a bit of code like this in the initialization if array[0] != sizeof (array) { I goofed }
this gives a safety in case I happen to 'forget' to change the value.
Erik
I solved the problem and at the same time simplified the code. What I was doing:
#define MENU_COUNT(N) (N) #define MENU_POS(X, Y) (X), (Y) char code menu_silly_putty[] = { MENU_COUNT(3), MENU_POS(1,2), MENU_POS(3,4), MENU_POS(5,6), };
This is great if you know for certain how many 'positions' you have but it's NOT programmatically correct if you don't. It leads to errors if you aren't paying attention to the number of menu's (the first char in the sequence). What I am doing now
#define MENU_POS(X, Y) { (X), (Y) } menu_position code menu_silly_putty[] = { MENU_POS(1,2), MENU_POS(3,4), MENU_POS(5,6), };
For the menu silly_putty definitions now for the actual list of menus
#define MENU_COUNT(N) (sizeof(N)/sizeof(menu_position)) #define MENU_DEF(N) { MENU_COUNT(N), &N } typedef struct menu_def { unsigned char menu_positions; menu_position code *positions; } menu_def; menu_def code menu_list[] = { MENU_DEF(menu_silly_putty), }
This seems to work and actually compiles more compactly than the goofy things I was doing before, mostly from the code to access it being simpler.
Stephen PS the actual code looks much better than this slop I'm just lazy.