In article <fotp1f$1l2h$1@ relay.tomsk.ru> , Roman Mashak <mrv@tusur.ruwr ote:
>Then why do both values look differently, in debugger:
>
>(gdb) p/x un
>$3 = {s = 0x102, c = {0x2, 0x1}}
>(gdb)
>
>(gdb) p/x un
>$3 = {s = 0x102, c = {0x2, 0x1}}
>(gdb)
(1) the bit pattern of the memory it's stored in
(2) the type of the "something"
The type is important because it defines how the bit pattern is
interpreted.
With a union of two different objects of the same size (in your case a
short occupying two bytes and an array of two chars occupying one byte
each), the implementation will usually use the same memory for both
(the limitations on what you as a programmer can do with a union allow
it to do this, and this behavior is what most nonportable uses of a
union depend on), so the bit pattern of the memory is the same for both
objects (and happens to be valid for both in this case; knowing that it
will be usually requires some non-portable knowledge of the system it's
running on).
So since the actual data is the same, the type of the object you're
accessing the data through is what determines what it looks like.
When the debugger looks at un.s, it interprets the two bytes as a short
and fgets the value 0x0102. When it looks at un.c, it interprets those
same two bytes as an array of char, and fgets two different values, one
for each char, giving {0x02, 0x01}.
In general, if you need to care about this, you're doing something
that's non-portable and comp.lang.c is usually not the right place to
ask. But the basics of how representations and values interact are
well within the scope of comp.lang.c (and because of that I think the
claim elsethread that your question should have been posted elsewhere
is unjustified).
dave
--
Dave Vandervies dj3vande at eskimo dot com
Erm... wouldn't clock(), used with Bill Godfrey's follow-up, ignoring my
follow-up to him (as suggested in your follow-up to me), do the trick
quite nicely? --Joona I Palaste in comp.lang.c
Leave a comment: