Setting union member in structure

Discussion in 'C++' started by Jeff Massung, Dec 22, 2003.

  1. Jeff Massung

    Jeff Massung Guest

    I am having a syntax issue that I hope someone else here knows how to
    rectify...

    I am loading an INI file and have a simple function to load values from
    it. The function is overloaded with the different return types int,
    double or string. A structure is defined:

    typedef struct INI_KEY {
    char strCategory[32];
    char strKey[32];

    union {
    int iDefaultValue;
    double dDefaultValue;
    char strDefaultValue[32];
    } def;
    } INI_KEY;

    In the implementation is a global array of INI_KEY's that list off
    various categories, keys and their default value (the value to return if
    it isn't in the INI):

    const INI_KEY g_keys[] = {
    { "category", "keyInt", 0 },
    { "category", "keyDouble", 0.0 },
    { "category", "keyString", "foo" },
    // ...
    };

    The problem I'm having is in the default value of the global array. The
    compiler is trying to convert the double and string values to integers
    and generating compiler errors. I'm sure there is a syntax to tell the
    compiler what member of the union to use, I just don't know it. If it
    makes a difference, I'm using MS VC++ 7.

    --
    Best regards,
    Jeff jma[at]mfire.com
    http://www.jm-basic.com
     
    Jeff Massung, Dec 22, 2003
    #1
    1. Advertising

  2. Jeff Massung

    Mike Wahler Guest

    "Jeff Massung" <> wrote in message
    news:...
    > I am having a syntax issue that I hope someone else here knows how to
    > rectify...
    >
    > I am loading an INI file and have a simple function to load values from
    > it. The function is overloaded with the different return types int,
    > double or string. A structure is defined:
    >
    > typedef struct INI_KEY {
    > char strCategory[32];
    > char strKey[32];
    >
    > union {
    > int iDefaultValue;
    > double dDefaultValue;
    > char strDefaultValue[32];
    > } def;
    > } INI_KEY;
    >
    > In the implementation is a global array of INI_KEY's that list off
    > various categories, keys and their default value (the value to return if
    > it isn't in the INI):
    >
    > const INI_KEY g_keys[] = {
    > { "category", "keyInt", 0 },
    > { "category", "keyDouble", 0.0 },
    > { "category", "keyString", "foo" },
    > // ...
    > };
    >
    > The problem I'm having is in the default value of the global array. The
    > compiler is trying to convert the double and string values to integers
    > and generating compiler errors. I'm sure there is a syntax to tell the
    > compiler what member of the union to use,


    There is not.

    > I just don't know it. If it
    > makes a difference, I'm using MS VC++ 7.


    =====================================================================
    ISO/IEC 14882:1998(E)

    8.5.1 Aggregates

    [....]

    15 When a union is initialized with a braceĀ­-enclosed initializer,
    the braces shall only contain an initializer for the first member
    of the union. [Example:

    union u { int a; char* b; };
    u a = { 1 };
    u b = a;
    u c = 1; // error
    u d = { 0, "asdf" }; // error
    u e = { "asdf" }; // error

    --end example] [Note: as described above, the braces around the
    initializer for a union member can be omitted if the union is
    a member of another aggregate. ]
    =====================================================================

    -Mike
     
    Mike Wahler, Dec 22, 2003
    #2
    1. Advertising

  3. Jeff Massung

    Jeff Massung Guest

    "Mike Wahler" <> wrote in
    news:c5GFb.1651$:

    >
    > "Jeff Massung" <> wrote in message
    > news:...
    >>
    >> const INI_KEY g_keys[] = {
    >> { "category", "keyInt", 0 },
    >> { "category", "keyDouble", 0.0 },
    >> { "category", "keyString", "foo" },
    >> // ...
    >> };
    >>
    >> The problem I'm having is in the default value of the global array.
    >> The compiler is trying to convert the double and string values to
    >> integers and generating compiler errors. I'm sure there is a syntax
    >> to tell the compiler what member of the union to use,

    >
    > There is not.
    >
    > =====================================================================
    > ISO/IEC 14882:1998(E)
    >
    > 8.5.1 Aggregates
    >
    > [....]
    >
    > 15 When a union is initialized with a braceĀ­-enclosed initializer,
    > the braces shall only contain an initializer for the first member
    > of the union. [Example:
    >
    > union u { int a; char* b; };
    > u a = { 1 };
    > u b = a;
    > u c = 1; // error
    > u d = { 0, "asdf" }; // error
    > u e = { "asdf" }; // error
    >
    > --end example] [Note: as described above, the braces around the
    > initializer for a union member can be omitted if the union is
    > a member of another aggregate. ]
    > =====================================================================
    >
    > -Mike
    >
    >


    Oh well. Thanks for the quick reply.

    --
    Best regards,
    Jeff jma[at]mfire.com
    http://www.jm-basic.com
     
    Jeff Massung, Dec 22, 2003
    #3
    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. Matt Garman
    Replies:
    1
    Views:
    693
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    931
    Chris Torek
    Apr 26, 2004
  3. csudha

    Difference between Structure & Union

    csudha, Sep 10, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,808
  4. Union nested in structure.

    , Feb 9, 2005, in forum: C Programming
    Replies:
    10
    Views:
    979
    Dave Thompson
    Feb 14, 2005
  5. deepak
    Replies:
    12
    Views:
    1,966
    Keith Thompson
    May 9, 2008
Loading...

Share This Page