I'm doing some low-level programming. For purpose of my task I need to initialize (sometimes hardware-limited) array. It can be just char[], but also unsigned short or whatever.
Most readable way is to use just some string of constant, known length. To ease the task I've wrote a macro to help myself.
#define INI( x ) { (x[0] << 8) | 0x00, (x[1] << 8) | 0x00 }
static const unsigned int tab[] = INI("ab");
int main(){
return 0;
}
Of course macro above is inside some #ifdef block and depends on architecture it's being build on. The problem I have is that I'm getting error:
initializer element is not constant
main.c:3: error: (near initialization for "tab[0]")
initializer element is not constant
main.c:3: error: (near initialization for "tab[1]")
But above code expands to:
static const unsigned int tab[] = { ("ab"[0] << 8) | 0x00, ("ab"[1] << 8) | 0x00 };
int main(){
return 0;
}
Each and EVERY element is not only constant at compile time, but also at preprocessor time. It could be even possible to create macro taking every character from the string and doing some manipulation (if only preprocessor would be able to get length of the string and would have some looping option of course).
So - why compiler isn't able to extract this information and what are my options? Any help is sincerely appreciated.
PS. I know that it works inside main() as
const unsigned int tab[] = INI("ab");
but I need it outside any function.
| 0x00?