2

In C, I have a statement like this:

unsigned char const Alpha[6][2] = (unsigned char)({
     { 0x90f }, { 0x92c, 0x940 }, { 0x938, 0x940 }, 
     { 0x921, 0x940 }, { 0x908 }, { 0x90f, 0x92b }
     });

But it produces an error as braced-group within expression allowed only inside a function.

Can anyone suggest a remedy.

NOTE:Alpha is a global constant and so it is outside any function.

Sibir
  • 313
  • 2
  • 6
  • 20
  • 1
    `(unsigned char)(....)` what you want here? – Jayesh Bhoi Aug 19 '14 at 09:49
  • I want to initialize the array inside those braces as shown @Jayesh – Sibir Aug 19 '14 at 09:50
  • I think It's not allowed by ANSI/ISO C nor C++ but gcc supports it.See some statement exprassion here https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html – Jayesh Bhoi Aug 19 '14 at 09:52
  • [possible help you can get from here](http://stackoverflow.com/questions/1238016/are-compund-statements-blocks-surrounded-by-parens-expressions-in-ansi-c) – Jayesh Bhoi Aug 19 '14 at 09:55
  • A cast here is required I think as the values here are too large for `unsigned char` @Jayesh – Sibir Aug 19 '14 at 09:56

2 Answers2

0

You don't need a compound literal and your values are too large for an unsigned char, change to:

unsigned short int const Alpha[][2] = {
     { 0x90f }, { 0x92c, 0x940 }, { 0x938, 0x940 }, 
     { 0x921, 0x940 }, { 0x908 }, { 0x90f, 0x92b }
};
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0
unsigned char const Alpha[6][2] = {
    { 0x90f & 0xFF }, { 0x92c & 0xFF , 0x940 & 0xFF }, { 0x938 & 0xFF, 0x940 & 0xFF },
    { 0x921 & 0xFF, 0x940 & 0xFF }, { 0x908 & 0xFF }, { 0x90f & 0xFF, 0x92b & 0xFF }
};
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70