Getting around VC's lack of support for C99 array initialisation designators - Page 1
Forum section: electronics / general

author: HwAoRrDk
author_rank: Super Contributor
timestamp: April 30, 2022, 03:01:22 pm
content: 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:static const int foo = { = 12345, = 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 compilerstilldoesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please addfullC99 support. Thank you.

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: April 30, 2022, 03:39:02 pm
content: Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pmWhat??? 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.Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pmThis works perfectly fine in Visual Studio. No problems whatsoever.Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pmWhat 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.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: April 30, 2022, 04:28:05 pm
content: 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:static const some_struct_t foo = { = { 2, 1, 1, some_func_ptr, some_other_func_ptr }, = { 2, 1, 1, some_func_ptr, some_other_func_ptr }, = { 2, 1, 1, some_func_ptr, some_other_func_ptr }, = { 3, 1, 1, some_func_ptr, some_other_func_ptr }, = { 3, 1, 1, some_func_ptr, some_other_func_ptr }, = { 2, 1, 1, some_func_ptr, some_other_func_ptr }, = { 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!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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: April 30, 2022, 06:00:52 pm
content: 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.

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: April 30, 2022, 08:22:12 pm
content: Quote from: HwAoRrDk on April 30, 2022, 04:28:05 pmI 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.