CTL icon indicating copy to clipboard operation
CTL copied to clipboard

Nested structure initialization causes syntax error

Open rminsk opened this issue 12 years ago • 2 comments

Is initialization of nested structures supported? Should it be a syntax error?

ctlversion 1;

struct Test {
  float a;
  float b;
};

Test buildTest(float a, float b)
{
  Test test = { a, b };
  return test;
}

struct NestedStruct {
  Test test;
  float v;
};

void testFunction()
{
  // This works
  NestedStruct a;
  a.test = buildTest(1.0, 2.0);
  a.v = 3.0;

  // This fails.  Should the error be syntax error?
  Test b = buildTest(1.0, 2.0);
  NestedStruct c = { b, 3.0 };

  // This fails.  Should the error be syntax error?
  NestedStruct d = { buildTest(1.0, 2.0), 3.0 };
}

rminsk avatar Sep 26 '13 22:09 rminsk

I'm not sure if section 5.6 of the spec applies.

There are also no explicit cast operators or function pointers. Side effects from expressions are limited to calls of functions with output parameters and the print statement. There are no assignment expressions – assignments occur in assignment statements only and cannot be nested.

but in section 5.4.2

Compound objects are initialized using nested lists enclosed in braces. The length and nesting of the initializer must exactly match the length and depth of the compound type being initialized (in contrast to the various initialization options supported in C).

rminsk avatar Sep 26 '13 23:09 rminsk

Looking at section 5.4.2 the following works:

NestedStruct e = { {1.0, 2.0}, 3.0 };

rminsk avatar Sep 26 '13 23:09 rminsk

based on your last comment, it sounds like you've resolved your issue. please reopen issue if needed

michaeldsmith avatar Dec 27 '22 05:12 michaeldsmith