By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Strange behaviour - Unions.

P: n/a
union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}
The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.

Sep 23 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

<si**************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);
Here, the expression 'u.b' gives undefined behavior,
since it was not the last union member assigned
return 0;
}
The above code prints:

134518260 134518260
200.00000 1128792064
Or it could have printed something else. Or nothing at all.
Or it could have done something completely arbitrary. This
is the nature of undefined behavior.
>
While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.
None whatsoever. In your program, 'u.b' has no defined value.

What are you really trying to do? Why do you think you need
a union?

-Mike
Sep 23 '06 #2

P: n/a
siddharth.mun...@gmail.com wrote:
union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}
The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.
The first line is actually the result of undefined behaviour.
The correct way to do it is
printf("%p %p\n",(void *)&u.a,(void *)&u.b);

As for the second line whatever explanation might
exist is specific to your implementation. Your compiler
stores at &u.a the sequence of bytes which correspond
to float 200 and then you ask the compiler to interpret
this sequence of bytes as an integer. Actually depending
on the width of int and float on your platform it may be
that the compiler is not taking into account all bytes when
evaluating u.b or takes into account additional garbage
bytes. The result obviously depends on how your platform
stores these two types.

Sep 23 '06 #3

P: n/a
si**************@gmail.com said:
union something
{
float a;
int b;
} u;
Ah, unions. Before you take something out, you must first put it in. If you
want to take out a float, you must put in a float. If you want to take out
an int, you must put in an int.

Here, you use static default initialiser rules for the file scope object, so
you are storing in the first-named object, a float. So the float has value
0.0f, and the value of the int is implementation-defined (and could be
anything, really).
>
int main()
Better, int main(void)
{
printf("%d %d\n",&u.a,&u.b);
The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also, you're
using %d %d when you should be using %f %d
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}
The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.
The value of b is implementation-defined if you store in a, and the
implementation is under no obligation to provide any kind of sensible rule.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 23 '06 #4

P: n/a

"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:P-******************************@bt.com...
si**************@gmail.com said:
[...]
>
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d
printf("%p %p\n",(void *)&u.a,(void *)&u.b);

[...]
--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Sep 23 '06 #5

P: n/a
Giorgio Silvestri said:
>
"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:P-******************************@bt.com...
>si**************@gmail.com said:

[...]
>>
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
>using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);
....adds nothing useful to the discussion that I can see. What point are you
trying to make?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 23 '06 #6

P: n/a
Richard Heathfield wrote:
Giorgio Silvestri said:

"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:P-******************************@bt.com...
si**************@gmail.com said:
[...]
>
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d
printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are you
trying to make?
You suggested %f and %d to print pointer expressions. His point is that
the proper way to print pointer expressions is to use %p and convert to
void *.

Sep 23 '06 #7

P: n/a
Harald van D?k said:
Richard Heathfield wrote:
>Giorgio Silvestri said:
>
"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:P-******************************@bt.com...
si**************@gmail.com said:
[...]
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are
you trying to make?

You suggested %f and %d to print pointer expressions.
Oh, good heavens, so I did. My apologies, Giorgio. I didn't even /see/ that
the OP was using &. I assumed (!) that he was trying to print the values of
the union members. Good spot.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 23 '06 #8

P: n/a

"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:SL********************@bt.com...
Harald van D?k said:
Richard Heathfield wrote:
Giorgio Silvestri said:


"Richard Heathfield" <in*****@invalid.invalidha scritto nel
messaggio
news:P-******************************@bt.com...
si**************@gmail.com said:


[...]


{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a
variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are
you trying to make?
You suggested %f and %d to print pointer expressions.

Oh, good heavens, so I did. My apologies, Giorgio. I didn't even /see/
that
the OP was using &. I assumed (!) that he was trying to print the values
of
the union members. Good spot.
no problem of course! :-)

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Sep 23 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.