Given that the Visual C++ compiler, when dealing with C code, doesn't have support for C99 array initialisation designators, does anybody have any clever ideas for initialising a sparsely-populated array?
What I'm talking about is the inability to do this:
static const int foo[256] = {
[64] = 12345,
[120] = 9876,
// etc...
};
I have a read-only lookup table 256-element array that will be static const, but a significant proportion of the indices will not be populated. I was just going to include an appropriate number of dummy entries in the initialisation statement, but maybe there's a better way.
P.S. Dear Microsoft, it's been 20 years, and your compiler still doesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please add full C99 support. Thank you.
Given that the Visual C++ compiler, when dealing with C code, doesn't have support for C99 array initialisation designators, does anybody have any clever ideas for initialising a sparsely-populated array?
What??? Visual Studio compiler supports C99 since a long time ago. It has full support for designated initializers in arrays since, like VS 2013 or 2015.
What I'm talking about is the inability to do this:
static const int foo[256] = {
[64] = 12345,
[120] = 9876,
// etc...
};
This works perfectly fine in Visual Studio. No problems whatsoever.
P.S. Dear Microsoft, it's been 20 years, and your compiler still doesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please add full C99 support. Thank you.
What are you going on about? Are you sure you enabled C11 ort C17 support in compiler settings? In old versions of Visual Studio support for C99 in C compiler had to be enabled semi-unofficially as a "language extension". In more recent Visual Studio it is simply enabled as official part of C11 or C17 support.
Whaaaaat!? :-//
I swear, it wasn't working for me! I'm using Visual Studio 2019. I even set the "C Language Standard" property of the specific .c file in question to C17. But for the following code, I was getting red underlined errors on the designators, giving an "initializer expected" error (or something like that), and it wouldn't build.
static const some_struct_t foo[0xFF] = {
[0x19] = { 2, 1, 1, some_func_ptr, some_other_func_ptr },
[0xA9] = { 2, 1, 1, some_func_ptr, some_other_func_ptr },
[0xB9] = { 2, 1, 1, some_func_ptr, some_other_func_ptr },
[0xC9] = { 3, 1, 1, some_func_ptr, some_other_func_ptr },
[0xD9] = { 3, 1, 1, some_func_ptr, some_other_func_ptr },
[0xE9] = { 2, 1, 1, some_func_ptr, some_other_func_ptr },
[0xF9] = { 1, 1, 1, some_func_ptr, some_other_func_ptr },
};
But just now I fiddled with the settings some more (didn't touch the code itself), changing things back and forth, and now suddenly the errors are gone! :wtf:
I can't imagine what caused this. So, I remembered that a while ago MSVC didn't fully support C99, and assumed it still didn't.