3 ms·
The first part about the name is just like C++: you use the name without `struct` unlike C where structs has its own namespace. That's what it's meant to illust
by lerno 4y ago
The first part about the name is just like C++: you use the name without `struct` unlike C where structs has its own namespace. That's what it's meant to illustrate.
The second question is more subtle. In C, the syntax is `struct { ... } [optional member name] ;`. Because there is no anonymous struct at the top level, the anonymous structs inside of a struct has a different syntax, also eschewing the final `;`, changing the syntax to `struct [optional member name] { ... }`. If the C syntax structure is desired a final `;` would be required. This syntax change comes from C2.
- addaon 4y agoBut what if I want to name the inner struct so I can refer to Outer::Inner (for example, for use with sizeof or similar, or to create a temporary to hold a copy of that type) later? Do I need to use typeof(Outer::inner)? And then of course multiple members of the same type...
- lerno 4y agoYou can't name the inner struct. So here are the options: Say that you want this from C: struct Foo { struct Bar { int x; int y; } bar; }; struct Foo f; f.bar = (struct Bar) { .x = 123 }; The struct in C3: struct Foo { struct bar { int x; int y; } }; Foo f; It is correct that we don't get the inner type, but because C3 has inference for {} assignment can happen despite that: f.bar = { .x = 123 }; // Valid in C3 You can grab the type using typeof if you want, but interestingly there's also the possibility to use structural casts, which are valid in C3: struct HelperStruct { int x; int y; } HelperStruct test = (HelperStruct)f.bar; // structural cast But the normal way would be to create a named struct outside if you really need a named struct, e.g.: struct Bar { int x, y; } struct Foo { Bar bar; };
- mst 4y agoThat ... actually seems reasonable? If you need it to be first class visible outside, you declare it outside. I believe I could live with this.
- addaon 4y agoAlso, just to be clear, the removal of semicolons deliberately makes the language whitespace sensitive, right? That is, struct Foo { } foo declares a variable of a new empty struct type, but struct Foo {} foo is a syntax error?
- lerno 4y agoC3 doesn't have declaration of structs/unions in variable declarations. So both are equivalent and are syntax errors.