3

I have following code which I guess is assigning a value to constant struct

in header file:

struct madStruct {
    uint8_t code;
    uint8_t cluster;
};
typedef struct madStruct MadStruct;

and in C file

const MadStruct madStructConst = {
    .code = 0x00,
    .cluster = 0x01,
};

I would like to know what what does this code supposed to do?

This code does not compile in Visual Studio C++ 2010, how can I convert it so I can compile in both MingW and Visual Studio C++ 2010?

1 Answer 1

8

The syntax was introduced in C99 and allows the name of the members to be explicitly specified at initialisation (the .code and .cluster are known as designators). The initialisation assigns the value 0x00 to the code member and value 0x01 to the cluster member.

VC only supports C89, so compiliation fails. As the struct only has two members and both are being initialised you can replace the initialisation with:

const MadStruct madStructConst = { 0x00, 0x01 };

without the designators the members are initialised with the specified values in the order that the members are defined in the struct. In this case code is assigned 0x00 and cluster is assigned 0x01, the same as the initialisation with the designators.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, I was guessing it is visual studio related thing.I was recently in Visual Studio 2012 launch event. they added lots of features to it but what is not changed is the compiler!! A Ferrari with WW Beetle 1945 engine!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.