AnsweredAssumed Answered

Initialising a const array by selected elements

Question asked by FridgeFreezer on Jul 23, 2010
Latest reply on Sep 7, 2010 by FridgeFreezer

I am trying to initialise (or initialize if you're American) a const array as a lookup table for a character map. It needs to have 256 elements but I don't need all of them to contain anything other than the default zero value.

 

K&R says you have to initialise every element in order:

 

static int number1[3] = { 5, 0, 7 };

 

 

IBM say that in these modern times you can initialise them like this:

 

static int number[3] = { [0] = 5, [2] = 7 };

 (From here: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/aryin.htm)

 

 

But if I do this:

 

static char lookup[256] = { ['A'] = 0x31, ['B'] = 0x32 };

 Codewarrior throws an error and calls it an illegal initialisation. Without the square brackets it complains that the hex bit is not an lvalue.

 

Can anyone tell me the correct way of doing this? I don't really want to have to type out the array in order and I would like to keep it as a const so it just sits in flash.

Outcomes