Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #127834
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: enums in structs |
| Date | 2018-03-14 17:57 -0700 |
| Organization | None to speak of |
| Message-ID | <lna7va88bg.fsf@kst-u.example.com> (permalink) |
| References | <a%iqC.2508$%a5.2417@fx21.am4> |
bartc <bc@freeuk.com> writes:
> I thought I knew most of the C quirks, but here's another one which I
> don't think I've seen before (perhaps with enum tags, not names):
>
> typedef struct {enum {red,green,blue} x;} S;
The typedef is irrelevant. This:
struct S {
enum { red, green, blue } x;
};
illustrates the same thing.
> Despite being declared inside the {...} of the struct, the scope of red,
> green and blue belong to the outer scope (the one containing the typedef).
Not all curly braces define scopes. Take a look at N1570 6.2.1 "Scopes
of identifiers". There's no such thing as "struct scope". The only
scopes are:
- Function scope (applies only to labels)
- File scope
- Block scope (refers to a block / compound statement)
- Function prototype scope (not often used)
Note that the member name "x" also has a wider scope than the struct.
But member names always have to be prefixed with "s." or "p->".
> Which also means that:
>
> typedef struct {enum {red,green,blue} x;} S;
> typedef struct {enum {red,green,blue} x;} T;
>
> won't work, as the names will clash.
Right.
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'd usually want to define the enum type separately -- or at least I
would.)
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
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