Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #127836
| Newsgroups | comp.lang.c |
|---|---|
| Date | 2018-03-14 20:04 -0700 |
| References | <a%iqC.2508$%a5.2417@fx21.am4> <lna7va88bg.fsf@kst-u.example.com> |
| Message-ID | <1af57651-5ef0-4ffd-95bd-4e73a2fbc4eb@googlegroups.com> (permalink) |
| Subject | Re: enums in structs |
| From | supercat@casperkitty.com |
On Wednesday, March 14, 2018 at 7:57:16 PM UTC-5, Keith Thompson wrote:
> If "red", "green", and "blue" had scopes restricted to the struct
> definition, you wouldn't be able to refer to their names outside the
> struct definition. C has no mechanism for prefixing an enumeration
> constant with the name of a type.
You can't refer to their names outside the context of an object of the
appropriate structure type. There are times it would be helpful to
make names of aggregate members available in the enclosing context.
The usefulness of anonymous structs and unions is somewhat undermined
by the fact that there is no way within a structure to refer to a subset
of members as a group while being able to also treat them individually
as members of the outer group. IMHO, there was a missed opportunity
to allow an "extern" qualifier on a struct member to serve as an
indication that the names should be exported to the enclosing context.
That would allow e.g.
struct point { int x,y; };
struct grafObj { struct point extern position; int color; };
struct grafObj myObj;
myObj.pt = objectOfTypePoint;
myObj.x++; // shorthand for myObj.position.x++;
As it is, C11 allows a struct within another struct to export its
members, but only if there's no way to treat that struct as a unit.
Consequently, the only thing that feature is good for is making
struct/union hybrids whose semantics end up being dodgy because C11
lacks any meaningful rules about when struct and union members of
non-character types can be used as lvalues.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
enums in structs bartc <bc@freeuk.com> - 2018-03-15 00:20 +0000
Re: enums in structs Keith Thompson <kst-u@mib.org> - 2018-03-14 17:57 -0700
Re: enums in structs supercat@casperkitty.com - 2018-03-14 20:04 -0700
Re: enums in structs Keith Thompson <kst-u@mib.org> - 2018-03-15 10:19 -0700
Re: enums in structs Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-14 20:07 -0700
Re: enums in structs David Brown <david.brown@hesbynett.no> - 2018-03-15 11:36 +0100
Re: enums in structs Thiago Adams <thiago.adams@gmail.com> - 2018-03-15 05:33 -0700
Re: enums in structs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-03-15 05:39 -0700
Re: enums in structs Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-15 06:35 -0700
Re: enums in structs jameskuyper@verizon.net - 2018-03-15 06:26 -0700
csiph-web