Author Topic: Getting around VC's lack of support for C99 array initialisation designators  (Read 602 times)

0 Members and 1 Guest are viewing this topic.

Offline HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1477
  • Country: gb
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:

Code: [Select]
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.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 151
  • Country: us
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:

Code: [Select]
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.

« Last Edit: April 30, 2022, 04:42:07 pm by TheCalligrapher »
 
The following users thanked this post: evb149

Offline HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1477
  • Country: gb
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.

Code: [Select]
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.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14476
  • Country: fr
It took a lot of time for MSVC to support C99, but it sure does these days. So unless you were using a version from 15 years ago or so.
Apparently the issue was with your settings, and possibly you either messed them up, or Visual Studio is buggy. But the compiler itself is alright.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 151
  • Country: us
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.

I hear that that IntelliSense "error predictor" with its red squiggles works pretty well these days, but personally I find it annoying. In my opinion it is better to just turn IntelliSense off and rely on regular compiler diagnostic messages.
« Last Edit: April 30, 2022, 08:35:59 pm by TheCalligrapher »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf