enum-type anonymous structs

Discussion in 'C Programming' started by andreyvul, Jan 3, 2008.

  1. andreyvul

    andreyvul Guest

    If I try compiling this in gcc, it says: "error: request for member
    `baz' in something not a structure or union".
    Any workarounds or tips on how to make a structure such that it
    behaves like an enum but its members can be addressed with '.'?
    I don't want to have to do this using #defines, as it would be far too
    messy.
    code:
    static struct {
    static const int baz = 1;
    } bar;

    void foo() {
    int x = bar.baz;
    }
    andreyvul, Jan 3, 2008
    #1
    1. Advertising

  2. andreyvul <> wrote:
    > If I try compiling this in gcc, it says: "error: request
    > for member `baz' in something not a structure or union".
    > Any workarounds or tips on how to make a structure such
    > that it behaves like an enum but its members can be
    > addressed with '.'?


    No.

    > I don't want to have to do this using #defines, as it
    > would be far too messy.
    > code:
    > static struct {
    >     static const int baz = 1;


    This is not legal C.

    > } bar;
    >
    > void foo() {
    >     int x = bar.baz;
    > }


    C++ probably has the feature you're looking for.

    Judicious naming conventions is as close as you'll come
    in C.

    --
    Peter
    Peter Nilsson, Jan 3, 2008
    #2
    1. Advertising

  3. andreyvul

    andreyvul Guest

    On Jan 2, 8:03 pm, Peter Nilsson <> wrote:
    > andreyvul <> wrote:
    > > If I try compiling this in gcc, it says: "error: request
    > > for member `baz' in something not a structure or union".
    > > Any workarounds or tips on how to make a structure such
    > > that it behaves like an enum but its members can be
    > > addressed with '.'?

    >
    > No.
    >
    > > I don't want to have to do this using #defines, as it
    > > would be far too messy.
    > > code:
    > > static struct {
    > > static const int baz = 1;

    >
    > This is not legal C.
    >
    > > } bar;

    >
    > > void foo() {
    > > int x = bar.baz;
    > > }

    >
    > C++ probably has the feature you're looking for.

    I know.
    > Judicious naming conventions is as close as you'll come
    > in C.

    :(

    So I'm stuck to #defines, then?


    > --
    > Peter
    andreyvul, Jan 3, 2008
    #3
  4. andreyvul

    Dan Henry Guest

    On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
    <> wrote:

    >If I try compiling this in gcc, it says: "error: request for member
    >`baz' in something not a structure or union".
    >Any workarounds or tips on how to make a structure such that it
    >behaves like an enum but its members can be addressed with '.'?
    >I don't want to have to do this using #defines, as it would be far too
    >messy.
    >code:
    >static struct {
    > static const int baz = 1;
    >} bar;
    >
    >void foo() {
    > int x = bar.baz;
    >}


    How does the functionality of enums and structures intersect?

    --
    Dan Henry
    Dan Henry, Jan 3, 2008
    #4
  5. andreyvul

    andreyvul Guest

    On Jan 2, 8:48 pm, Dan Henry <> wrote:
    > How does the functionality of enums and structures intersect?


    enums are lists of constants, correct?
    What if the list could be accessed using struct format, like
    enum.member?
    andreyvul, Jan 3, 2008
    #5
  6. andreyvul <> wrote:
    > Peter Nilsson <> wrote:
    > > andreyvul <> wrote:
    > > > ...Any workarounds or tips on how to make a structure
    > > > such that it behaves like an enum but its members can
    > > > be addressed with '.'?

    > >
    > > No.
    > >
    > > > I don't want to have to do this using #defines, as it
    > > > would be far too messy.
    > > > code:
    > > > static struct {
    > > >     static const int baz = 1;

    > >
    > > This is not legal C.
    > >
    > > > } bar;
    > > >
    > > > void foo() {
    > > >     int x = bar.baz;
    > > > }

    > >
    > > C++ probably has the feature you're looking for.

    >
    > I know.
    >
    > > Judicious naming conventions is as close as you'll come
    > > in C.

    >
    > :(
    >
    > So I'm stuck to #defines, then?


    Unlike your #define (which I'm not sure how you see it
    operating), naming conventions is established practice,
    so it's easier to maintain.

    Perhaps if you explain the real problem, in partiuclar why
    you need a static member of a struct, then we can suggest
    better alternatives.

    --
    Peter
    Peter Nilsson, Jan 3, 2008
    #6
  7. andreyvul

    Dan Henry Guest

    On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
    <> wrote:

    >On Jan 2, 8:48 pm, Dan Henry <> wrote:
    >> How does the functionality of enums and structures intersect?

    >
    >enums are lists of constants, correct?
    >What if the list could be accessed using struct format, like
    >enum.member?
    >

    Well you've got me there. What if? I don't appreciate the value of
    accessing "the enum list" using struct (plus member?) format. More
    likely is that I don't understand the problem you are trying to solve.
    Quite likely is that C does not, other than possibly by preprocessing
    means, provide what you are looking for.

    --
    Dan Henry
    Dan Henry, Jan 3, 2008
    #7
  8. andreyvul

    Thad Smith Guest

    Dan Henry wrote:
    > On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
    > <> wrote:
    >
    >> On Jan 2, 8:48 pm, Dan Henry <> wrote:
    >>> How does the functionality of enums and structures intersect?

    >> enums are lists of constants, correct?
    >> What if the list could be accessed using struct format, like
    >> enum.member?
    >>

    > Well you've got me there. What if? I don't appreciate the value of
    > accessing "the enum list" using struct (plus member?) format. More
    > likely is that I don't understand the problem you are trying to solve.


    I suspect that the OP is trying to introduce a name space so that he can
    have different flavors of foo by using the enclosing struct name.

    What I do is to prefix enumeration constants for an abbreviation associated
    with the category:

    enum bar {
    BAR_FOO,
    BAR_BAZ
    };

    --
    Thad
    Thad Smith, Jan 3, 2008
    #8
  9. andreyvul <> writes:
    > If I try compiling this in gcc, it says: "error: request for member
    > `baz' in something not a structure or union".
    > Any workarounds or tips on how to make a structure such that it
    > behaves like an enum but its members can be addressed with '.'?
    > I don't want to have to do this using #defines, as it would be far too
    > messy.
    > code:
    > static struct {
    > static const int baz = 1;
    > } bar;
    >
    > void foo() {
    > int x = bar.baz;
    > }


    Here's what I get with gcc 4.1.3:

    c.c:2: error: expected specifier-qualifier-list before 'static'
    c.c: In function 'foo':
    c.c:6: error: 'struct <anonymous>' has no member named 'baz'

    Are you sure you didn't get an error message on the declaration of
    "baz"? (I got no errors when I compiled it as C++.)

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 3, 2008
    #9
  10. andreyvul

    Jack Klein Guest

    On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
    <> wrote in comp.lang.c:

    > If I try compiling this in gcc, it says: "error: request for member
    > `baz' in something not a structure or union".
    > Any workarounds or tips on how to make a structure such that it
    > behaves like an enum but its members can be addressed with '.'?
    > I don't want to have to do this using #defines, as it would be far too
    > messy.
    > code:
    > static struct {
    > static const int baz = 1;
    > } bar;
    >
    > void foo() {
    > int x = bar.baz;
    > }


    A few others have questioned why you want to do it, but I haven't seen
    anyone post a suggestion like this:

    struct bar { int baz; };

    static const struct bar bar = { 1 };

    void foo(void)
    {
    int x = bar.baz;
    }

    You just need to define the type, and than a const initialized (and
    static, if you want) object of the type separately.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 3, 2008
    #10
  11. "andreyvul" <> wrote in message
    news:...
    > On Jan 2, 8:48 pm, Dan Henry <> wrote:
    > > How does the functionality of enums and structures intersect?

    >
    > enums are lists of constants, correct?
    > What if the list could be accessed using struct format, like
    > enum.member?


    enums are something like compile time constants in C. But it does not
    introduce a
    namespace. For example the folowing code gave me errors..

    #include <stdio.h>

    enum RainBColor {
    VIOLET = 0,
    INDIGO = 1,
    BLUE = 2,
    ..

    };
    enum Color {
    WHITE = 0;
    BLUE = 1;

    };
    int main(void)
    {
    enum Color color = BLUE;
    printf("color has the integer value = %d\n",(int)color);
    return 0;
    }

    color.c:12: error: conflicting types for `BLUE'
    color.c:7: error: previous declaration of `BLUE'

    Only way out in C is to use a prefix like RB_BLUE and COL_BLUE. Sure it
    would be nice if C had let us
    refer the enum consts in someways like

    1) Color.BLUE,RainBColor.BLUE // I think PASCAL has this method.
    2) Color::BLUE and RainBColor::Blue

    It would sure make for readability as clarity.

    Suppose we use const structs, we lose the "type info" since we have to refer
    to the variable as plain "ints".

    typedef const struct {
    unsigned char WHITE = 0;
    unsigned char BLUE = 1;
    ..
    } Color;

    typedef const struct {
    unsigned char VIOLET = 0;
    unsigned charu INDIGO = 1;
    unsigned char BLUE = 2;
    ..
    };

    int main(void)
    {
    unsigned char color = RainBColor.BLUE; /* But type is now "unsigned
    char" */

    }
    Ravishankar S, Jan 3, 2008
    #11
  12. "Ravishankar S" <> wrote in message
    news:flhr6q$gd0$...
    > "andreyvul" <> wrote in message
    > news:...
    > > On Jan 2, 8:48 pm, Dan Henry <> wrote:
    > > > How does the functionality of enums and structures intersect?

    > >
    > > enums are lists of constants, correct?
    > > What if the list could be accessed using struct format, like
    > > enum.member?

    >
    > enums are something like compile time constants in C. But it does not
    > introduce a
    > namespace. For example the folowing code gave me errors..
    >
    > #include <stdio.h>
    >
    > enum RainBColor {
    > VIOLET = 0,
    > INDIGO = 1,
    > BLUE = 2,
    > ..
    >
    > };
    > enum Color {
    > WHITE = 0;
    > BLUE = 1;
    >
    > };
    > int main(void)
    > {
    > enum Color color = BLUE;
    > printf("color has the integer value = %d\n",(int)color);
    > return 0;
    > }
    >
    > color.c:12: error: conflicting types for `BLUE'
    > color.c:7: error: previous declaration of `BLUE'
    >
    > Only way out in C is to use a prefix like RB_BLUE and COL_BLUE. Sure it
    > would be nice if C had let us
    > refer the enum consts in someways like
    >
    > 1) Color.BLUE,RainBColor.BLUE // I think PASCAL has this method.
    > 2) Color::BLUE and RainBColor::Blue
    >
    > It would sure make for readability and clarity.
    >
    > Suppose we use const structs, we lose the "type info" since we have to

    refer
    > to the variable as plain "ints". It also involves memory for the const

    structs..
    >
    > const struct {
    > unsigned char WHITE = 0;
    > unsigned char BLUE = 1;
    > ..
    > } Color;
    >
    > const struct {
    > unsigned char VIOLET = 0;
    > unsigned char INDIGO = 1;
    > unsigned char BLUE = 2;
    > ..
    > }RainBColor;
    >
    > int main(void)
    > {
    > unsigned char color = RainBColor.BLUE; /* But type is now "unsigned
    > char" */
    >
    > }
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    Ravishankar S, Jan 3, 2008
    #12
  13. andreyvul wrote:
    > If I try compiling this in gcc, it says: "error: request for member
    > `baz' in something not a structure or union".


    That's strange. My copy of gcc gives a number of messages. See below.

    > Any workarounds or tips on how to make a structure such that it
    > behaves like an enum but its members can be addressed with '.'?


    structs are not enums. It doesn't even make sense for them to "behave
    like an enum."

    >
    >
    > static struct {
    > static const int baz = 1;
    > } bar;
    >
    > void foo() {
    > int x = bar.baz;
    > }


    Notice the diagnostics gcc gives me for your code:
    3: error: expected specifier-qualifier-list before 'static'
    4: warning: struct has no members
    In function 'foo':
    8: error: 'struct <anonymous>' has no member named 'baz'
    8: warning: unused variable 'x'

    These arise because there is no such thing as a static member in a C
    struct. Perhaps you wanted to use some other language.
    Martin Ambuhl, Jan 3, 2008
    #13
  14. andreyvul

    andreyvul Guest

    On Jan 3, 2:12 am, Martin Ambuhl <> wrote:
    > andreyvul wrote:
    > > If I try compiling this in gcc, it says: "error: request for member
    > > `baz' in something not a structure or union".

    >
    > That's strange. My copy of gcc gives a number of messages. See below.
    >
    > > Any workarounds or tips on how to make a structure such that it
    > > behaves like an enum but its members can be addressed with '.'?

    >
    > structs are not enums. It doesn't even make sense for them to "behave
    > like an enum."
    >
    > >
    > >

    >
    > > static struct {
    > > static const int baz = 1;
    > > } bar;

    >
    > > void foo() {
    > > int x = bar.baz;
    > > }

    >
    > Notice the diagnostics gcc gives me for your code:
    > 3: error: expected specifier-qualifier-list before 'static'
    > 4: warning: struct has no members
    > In function 'foo':
    > 8: error: 'struct <anonymous>' has no member named 'baz'
    > 8: warning: unused variable 'x'
    >
    > These arise because there is no such thing as a static member in a C
    > struct. Perhaps you wanted to use some other language.


    I used structs so that I could namespace constants, namely error
    codes.
    andreyvul, Jan 3, 2008
    #14
  15. andreyvul

    andreyvul Guest

    On Jan 2, 11:50 pm, Jack Klein <> wrote:
    > On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
    > <> wrote in comp.lang.c:
    >
    > > If I try compiling this in gcc, it says: "error: request for member
    > > `baz' in something not a structure or union".
    > > Any workarounds or tips on how to make a structure such that it
    > > behaves like an enum but its members can be addressed with '.'?
    > > I don't want to have to do this using #defines, as it would be far too
    > > messy.
    > > code:
    > > static struct {
    > > static const int baz = 1;
    > > } bar;

    >
    > > void foo() {
    > > int x = bar.baz;
    > > }

    >
    > A few others have questioned why you want to do it, but I haven't seen
    > anyone post a suggestion like this:
    >
    > struct bar { int baz; };
    >
    > static const struct bar bar = { 1 };
    >
    > void foo(void)
    > {
    > int x = bar.baz;
    >
    > }
    >
    > You just need to define the type, and than a const initialized (and
    > static, if you want) object of the type separately.


    Fails if you do this:
    void foo(int x) {
    switch (x) {
    case bar.baz:
    ...
    }
    }
    andreyvul, Jan 3, 2008
    #15
  16. andreyvul

    andreyvul Guest

    On Jan 3, 12:46 pm, andreyvul <> wrote:
    > I used structs so that I could namespace constants, namely error
    > codes.


    To use as cases in a switch(setjmp(buf)) statement.
    andreyvul, Jan 3, 2008
    #16
  17. andreyvul

    Chris Dollin Guest

    andreyvul wrote:

    > I used structs so that I could namespace constants, namely error
    > codes.


    It has presumably become apparent to you that you can't do
    this in C; you're essentially stuck with naming conventions.
    Just pick good names and wash thoroughly and you'll be OK.

    --
    Preprocessor Hedgehog
    "Who do you serve, and who do you trust?" /Crusade/
    Chris Dollin, Jan 3, 2008
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    625
    Al Bowers
    Apr 5, 2004
  2. Eric Sosman
    Replies:
    3
    Views:
    771
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    486
  4. Lew
    Replies:
    0
    Views:
    527
  5. Roedy Green
    Replies:
    0
    Views:
    720
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page