#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Why isn't my union being assigned?


    typedef union {
    short count;
    float weight;
    float volume;
    } quantity;

    quantity q = {.weight = 1.5};

    I copied this right from the book. My compiler says "expected an expression" by the dot character. Does it not allow this way to assign union values?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    It means your compiler is probably some old fossil from another time - possibly before you were even born.
    Code:
    $ cat foo.c
    typedef union {
      short count;
      float weight;
      float volume;
    } quantity;
    
    int main()
    {
      quantity q = {.weight = 1.5};
      return 0;
    }
    $ gcc -ansi -pedantic foo.c
    foo.c: In function ‘main’:
    foo.c:9:17: warning: ISO C90 forbids specifying subobject to initialize [-pedantic]
    $ gcc foo.c
    $ gcc -std=c99 foo.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Visual Studio 2010 isn't that old nor is older than me..
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    and when I assign it like this:

    typedef union {
    char *name;
    short weight;
    short capacity;
    } BIKE
    BIKE bicycle;
    bicycle.weight = 2;

    it assigns weight fine.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > Visual Studio 2010 isn't that old nor is older than me..
    Visual Studio (any version) does NOT support the C99 standard.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    ..Why not? That's stupid.

    Should I get a new compiler because of this?
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > ..Why not? That's stupid.
    That's Microsoft's business decision not to support C99.

    > Should I get a new compiler because of this?
    Well how many C99 (and perhaps C11) features are burning "must haves" for you?

    As a C89 compiler, it's a very good compiler, and the debugger is first class.

    If your book doesn't mention that "designated initialisers" are a C99 feature, then perhaps you need another book.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by miz6565
    ..Why not? That's stupid.

    Should I get a new compiler because of this?
    Only if your instructors require you to use C99. So far I don't see it being used in industry with ANSI C (AKA C89) being preferred for C programs and C++ being the upscaling path; for the special things that C99 does, C++ already does most of that. Microsoft is investing in the future of C++ rather than indulging passing fads.

    Apparently, C99 is being pushed in some of the schools. It would be interesting to find out how much it's used in the real world, in industry.

IMN logo majestic logo threadwatch logo seochat tools logo