Weird syntax in inherited code that compiler dislikes

Viewed 72

I have inherited this code

static const UCHAR g_ReportSizes[] = {
  [0x00] = 0,
  [0x01] = 4,
  [0x02] = 2,
  [0x03] = 5,
  [0x04] = 2,
  [0x07] = 4,
  [0x09] = 5,
  [0x20] = 64,
  [0x05] = 3,
  [0x06] = 3,
  [0x14] = 4,
  [0x08] = 4,
  [0x15] = 2,
};

And get the following error when I try to compile it

1>c:\source\test\test.c(40) : error C2059: syntax error : '['

For the line [x00] = 0,

I’ve never seen an initializer like that before. How do I resolve this error?

1 Answers

Your compiler does not support the C99 syntax for designated initializers introduced in the C language more than 22 years ago.

Microsoft decided to delay C99 support for many years and tried to only introduce features common to both C99 and C++.

You might get better support with an updated version of their Visual Studio and compiling for C explicitly instead of C++.

Alternately, you could change the initializer to fix the issue but there are likely many other places where the programmer used modern features not supported by your legacy tool set.

It is also a bit surprising to see this definition for g_ReportSizes specifying only 33 values instead of 256.

Here is an equivalent pre-c99 initializer:

static const UCHAR g_ReportSizes[] = {
    0, // 0x00
    4, // 0x01
    2, // 0x02
    5, // 0x03
    2, // 0x04
    3, // 0x05
    3, // 0x06
    4, // 0x07
    4, // 0x08
    5, // 0x09
    0, // 0x0A
    0, // 0x0B
    0, // 0x0C
    0, // 0x0D
    0, // 0x0E
    0, // 0x0F
    0, // 0x10
    0, // 0x11
    0, // 0x12
    0, // 0x13
    4, // 0x14
    2, // 0x15
    0, // 0x16
    0, // 0x17
    0, // 0x18
    0, // 0x19
    0, // 0x1A
    0, // 0x1B
    0, // 0x1C
    0, // 0x1D
    0, // 0x1E
    0, // 0x1F
    64, // 0x20
};
Related