In C (NOT C++), I am trying to create two string tables that contain the same values, but have the values sorted in two different ways. And I don't want the strings to be duplicated in memory.
Basically, I want to do the following. Except according to gcc, it fails because "initializer element is not constant" in the second array initialization. Is there some way around this problem? Preferably without saying "oh, well the compiler should optimize it to do what you want"?
static const char * monthNames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/********
* Month table sorted for O(log N) string lookup
*/
static const char * monthSortedKeys[]= {
monthNames[3], /* Apr */
monthNames[7], /* Aug */
monthNames[11], /* Dec */
monthNames[1], /* Feb */
monthNames[0], /* Jan */
monthNames[6], /* Jul */
monthNames[5], /* Jun */
monthNames[2], /* Mar */
monthNames[4], /* May */
monthNames[10], /* Nov */
monthNames[9], /* Oct */
monthNames[8] /* Sep */
};
Clarification: I know how to do this with a loop. I'm trying to figure out how to do it at compile time.
Another Update: I just compiled this as C++ (g++) and it works. But again, I'm looking for the C answer.