Connecting Tech Pros Worldwide Forums | Help | Site Map

Initiate a union

martin
Guest
 
Posts: n/a
#1: Nov 15 '05
I want to initiate a union and and have the problem that I sometimes
need to use float and sometimes an int. The float values (u.fval) turns
up ok but not the ints (u.ival).

I tried typecating it as below but it does not seem to do the trick.

Would be happy if anyone has a solution for this problem.

struct str {
int i;
float f;

typedef union {
float fval;
int ival;
} u;

} tStruct;

tStruct structArray[] = {
{ 0, 0.0 , 3.2},
{ 0, 0.0 , (float)(int)9}
};


Martin Ambuhl
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Initiate a union


martin wrote:[color=blue]
> I want to initiate a union and and have the problem that I sometimes
> need to use float and sometimes an int. The float values (u.fval) turns
> up ok but not the ints (u.ival).
>
> I tried typecating it as below but it does not seem to do the trick.
>
> Would be happy if anyone has a solution for this problem.
>
> struct str {
> int i;
> float f;
>
> typedef union {
> float fval;
> int ival;
> } u;
>
> } tStruct;[/color]

The above is hopelessly mangled. See the example below.[color=blue]
>
> tStruct structArray[] = {
> { 0, 0.0 , 3.2},
> { 0, 0.0 , (float)(int)9}
> };
>[/color]

Here is a C99-oriented way to do this:
#include <stdio.h>
typedef struct
{
int i;
float f;
union
{
float fval;
int ival;
} u;

} tStruct;


int main(void)
{
tStruct structArray[] = {
{.u.fval = 3.2},
{.u.ival = 9}
};
printf("structArray[0].u.fval contains %g, expected 3.2\n",
structArray[0].u.fval);
printf("structArray[1].u.ival contains %d, expected 9\n",
structArray[1].u.ival);
return 0;
}


[output]
structArray[0].u.fval contains 3.2, expected 3.2
structArray[1].u.ival contains 9, expected 9
gladiator
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Initiate a union



If we want to initialize i, f and union u ?
How to do that

usr.root@gmail.com
Guest
 
Posts: n/a
#4: Nov 15 '05

re: Initiate a union



gladiator 写道:
[color=blue]
> If we want to initialize i, f and union u ?
> How to do that[/color]


you can do it like this
tStruct structArray[] = {
{1,2.3,.u.fval = 3.2},
{2,3,4,.u.ival = 9}
};
i have test it!

martin
Guest
 
Posts: n/a
#5: Nov 15 '05

re: Initiate a union


Thanks for the quick response but when I tried your solution my
compiler gave me the following error:

Serious error: <expression> expected but found '.'

Default User
Guest
 
Posts: n/a
#6: Nov 15 '05

re: Initiate a union


martin wrote:
[color=blue]
> Thanks for the quick response but when I tried your solution my
> compiler gave me the following error:[/color]

See below. You also failed to show us what you actually tried. We have
not idea what didn't work.



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Walter Roberson
Guest
 
Posts: n/a
#7: Nov 15 '05

re: Initiate a union


In article <1127440521.318123.145840@o13g2000cwo.googlegroups .com>,
usr.root@gmail.com <usr.root@gmail.com> wrote:
[color=blue]
>gladiator =E5=86=99=E9=81=93=EF=BC=9A[/color]
[color=blue][color=green]
>> If we want to initialize i, f and union u ?
>> How to do that[/color][/color]
[color=blue]
>you can do it like this
> tStruct structArray[] = {
> {1,2.3,.u.fval = 3.2},
> {2,3,4,.u.ival = 9}
> };
>i have test it![/color]

You didn't test it with a C89 compiler.
--
Camera manufacturers have temporarily delayed introduction of
sub-millibarn resolution bio-hyperdimensional plasmatic space polyimaging,
but indications are that is still just around the corner.
Closed Thread


Similar C / C++ bytes