Initialising a const array by selected elements

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Initialising a const array by selected elements

Jump to solution
1,208 Views
FridgeFreezer
Senior Contributor I

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/langua...)

 

 

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.

Labels (1)
0 Kudos
1 Solution
372 Views
CompilerGuru
NXP Employee
NXP Employee

Which architecture are you targeting, which version of CW are you using (eclipse based or not), which derivative. Without such information I can only hint you to the solution.

 

The syntax which explicitly states which element to set was introduced with C99, so depending on what you use (I don't know :smileysad: ), you have to enable the C99 support via preference or command line.

 

In general, if you want to stay as compatible as possible, I would stick with the old C89 initialization style.

 

Daniel

View solution in original post

0 Kudos
3 Replies
373 Views
CompilerGuru
NXP Employee
NXP Employee

Which architecture are you targeting, which version of CW are you using (eclipse based or not), which derivative. Without such information I can only hint you to the solution.

 

The syntax which explicitly states which element to set was introduced with C99, so depending on what you use (I don't know :smileysad: ), you have to enable the C99 support via preference or command line.

 

In general, if you want to stay as compatible as possible, I would stick with the old C89 initialization style.

 

Daniel

0 Kudos
372 Views
FridgeFreezer
Senior Contributor I

Apologies, I mistakenly assumed the question was quite general. I'm using CW7.2 (EWL C) for ColdFire, target device is MCF52259. I will try turning on C99 extensions.

0 Kudos
372 Views
FridgeFreezer
Senior Contributor I

Well turning on C99 extensions caused so many errors with the rest of the code base we're importing (from the previous M-Core / CW5.9 based project) that I gave up with it :smileyindifferent: as it'd be far more work to correct everything.

 

Looks like I'll just have to populate the array in its entirity the long way.

0 Kudos