0

Is ther a way to use the names of the struct-members for initial a const instance

typedef struct {
    int i1;
    int i2;
    int i3;
} info_t;

//- GCC
const info_t info = {
  .i1 = 1,
  .i2 = 2
}

//- VS
const info_t info = {1,2,0);

the GCC allows this handy way but Visual Studio causes a error C2143 "Syntax error: missing } before."... GCC also allows to omit members (see example: info.t3 is not set)

Does anyone know a easy way to produce compatible and easy to read code with a workaround for VS?

Thomas
  • 2,345
  • 1
  • 18
  • 17

1 Answers1

1

It's called designated initializer which is introduced in C99.

But Visual Studio doesn't have support for C99 right now, so, no, you can't do it in Visual Studio then, you have to stick to the C89 way:

const info_t info = {1,2,0);

However, according to MSDN and Infoq on the roadmap of Visual Studio, there will be some support for C99 in Visual Studio 2013 RTM, and this feature is one of them.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • @Thomas According to http://blogs.msdn.com/b/vcblog/archive/2013/06/28/c-11-14-stl-features-fixes-and-breaking-changes-in-vs-2013.aspx, there will be some support for C99 in Visual Studio 2013, I don't know if this feature will be one of them. – Yu Hao Aug 25 '13 at 14:21
  • @YuHao, _Additionally, some C99 Core Language features will be implemented in 2013 RTM: * C99 _Bool * C99 compound literals * C99 designated initializers * C99 variable declarations_ ([also see this](http://www.infoq.com/resource/news/2013/07/vs2013_CPP_compliance/en/resources/VC_Roadmap.png)) – chris Aug 25 '13 at 14:24
  • @chris Nice, I'm just going to add this to the answer. – Yu Hao Aug 25 '13 at 14:26
  • @Thomas If you are not stuck with VS, you could try using another compiler that supports C99, e.g. GCC with [MINGW](http://www.mingw.org/). – LorenzoDonati4Ukraine-OnStrike Aug 25 '13 at 14:34
  • @Lorenzo Donati: unfortunately I'm stuck with VS!... but thank you so far... I try to use a compiler-switch :-( – Thomas Aug 25 '13 at 14:37
  • 1
    @Thomas try to see if the macro hacks [here](http://stackoverflow.com/questions/5440611/how-to-rewrite-c-struct-designated-initializers-to-c89-resp-msvc-c-compiler) may help you. – LorenzoDonati4Ukraine-OnStrike Aug 25 '13 at 14:52