3

I have this structure:

typedef struct
{
    union{
        int bytex[8];
        int bytey[7];
   }Value ;
   int cod1;
   int cod;
} test;

and want to initialize the constant test as follow:

const test T{
.Value.bytex = {0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44},
.cod1=0,
.cod=1,
};

I am getting the following error

Expected primary-expression before '.' token

This initialization is however correct:

const test T{
{0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44},
.cod1=0,
.cod=1,
};

Do you have any Idea ?

0

1 Answer 1

4

First of all, this isn't close to ressembling struct/union initialization syntax. Fix:

const test T = 
{
  .Value.bytex = { 0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44 },
  .cod1 = 0,
  .cod  = 1,
};

Second, if you have the option to use standard C, you can drop the inner variable name:

typedef struct
{
  union {
    int bytex[8];
    int bytey[7];
  };
  int cod1;
  int cod;
} test;

const test T = 
{
  .bytex = { 0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44 },
  .cod1 = 0,
  .cod  = 1,
};
Sign up to request clarification or add additional context in comments.

1 Comment

@FiddlingBits Nope, could be anywhere. This is the C11 anonymous struct/union feature.

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.