The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Why isn't my union being assigned?
Discuss Why isn't my union being assigned? in the C Programming forum on Dev Shed. Why isn't my union being assigned? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 16th, 2013, 11:07 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 40
Time spent in forums: 8 h 16 m 35 sec
Reputation 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?
|

March 16th, 2013, 11:58 AM
|
 |
Contributed User
|
|
|
|
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
|

March 16th, 2013, 12:01 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 40
Time spent in forums: 8 h 16 m 35 sec
Reputation Power: 0
|
|
|
Visual Studio 2010 isn't that old nor is older than me..
|

March 16th, 2013, 12:03 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 40
Time spent in forums: 8 h 16 m 35 sec
Reputation 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.
|

March 16th, 2013, 12:11 PM
|
 |
Contributed User
|
|
|
|
|
> Visual Studio 2010 isn't that old nor is older than me..
Visual Studio (any version) does NOT support the C99 standard.
|

March 16th, 2013, 12:20 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 40
Time spent in forums: 8 h 16 m 35 sec
Reputation Power: 0
|
|
|
..Why not? That's stupid.
Should I get a new compiler because of this?
|

March 16th, 2013, 01:44 PM
|
 |
Contributed User
|
|
|
|
> ..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.
|

March 16th, 2013, 01:57 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|