Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #127863

Re: enums in structs

Newsgroups comp.lang.c
Date 2018-03-15 05:39 -0700
References <a%iqC.2508$%a5.2417@fx21.am4>
Message-ID <59cd7518-e27a-4251-8732-fd03101a8cec@googlegroups.com> (permalink)
Subject Re: enums in structs
From "Rick C. Hodgin" <rick.c.hodgin@gmail.com>

Show all headers | View raw


On Wednesday, March 14, 2018 at 8:20:30 PM UTC-4, Bart wrote:
> 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;
> 
> 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).
> 
> 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.
> 
> The construct cropped up in the new sources of Tiny C, and it didn't 
> work (with my compiler) because the use of the enum name (not 'red' but 
> somewhat longer) was not recognised. (Silly me, I'd assumed the names 
> were local to the struct being declared.)
> 
> This appears rather crude behaviour for a language that makes a big deal 
> about VLAs and name reuse inside functions via unlimited block scopes.
> 
> ----------------------------------
> 
> (How does my language deal with it? Apparently, much better; enum names 
> are properly local; the second example works; but you have to access 
> them as S.red or T.red. You don't need (can't) declare the dummy x field.
> 
> So I can implement a feature better than C can, without even trying! As 
> I wasn't aware this was even possible. I do however understand how 
> lexical scope must work)
> 
> -- 
> bartc

I agree the enum should be scoped.  If there were ever a name
collision the developer could use the scope modifier to hone it
down.

But, I also think it should be contextually aware.  If I'm refer-
encing something that relates to that structure, then use the
local name scope enum by default, and possibly generate a diagnostic
on the assumption, but it shouldn't require rocket science to nail
these things down... just common sense.

-- 
Rick C. Hodgin

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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