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

How to extract bytes from a long?

P: n/a
I'm suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];
} X;

void main( void )
{
int i; // Always need an 'i', ...
long val = 0x12345678;

X.lng = val;
for( i = 3; i >= 0; i-- )
{
printf( "%#02x\n", X.byt[i] );
}
} // main
Nov 13 '05 #1
Share this Question
Share on Google+
45 Replies


P: n/a
Curt Geske wrote:
I'm suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];
} X;

void main( void )
{
int i; // Always need an 'i', ...
long val = 0x12345678;

X.lng = val;
for( i = 3; i >= 0; i-- )
{
printf( "%#02x\n", X.byt[i] );
}
} // main


We wanted solutions that didn't exhibit undefined behaviour, ta very
much. Or even unspecified behaviour - like, is X.byt[0] 0x12 or 0x78
or 0x34 or 0x56, and why?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #2

P: n/a
Curt Geske <cg**********@compuserve.com> wrote:

If you want to reply to a post, please use the "Reply" function of your
newsreader and do not start a new thread. Thank you.
I'm suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];


Stop right here: what makes you think a long is four chars wide?

<SNIP>

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3

P: n/a
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:bk********************************@4ax.com...
I'm suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];


Stop right here: what makes you think a long is four chars wide?


That could be easily overcome with sizeof. It's more that you can't rely on
the byte order if you try to do it portably.
Nov 13 '05 #4

P: n/a
Serve La wrote:

"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:bk********************************@4ax.com...
I'm suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];


Stop right here: what makes you think a long is four chars wide?


That could be easily overcome with sizeof. It's more that you can't rely on
the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 13 '05 #5

P: n/a
Curt Geske wrote:
I'm suprised no one suggested a union!
Since your suggestion involves non-portable implementation-defined behavior
(in addition to the obvious illiteracy for the return type for main), I'm
surprised that anyone would suggest it.

#include <stdio.h>
union _x
{
long lng;
char byt[4];
} X;

void main( void )
I'm surprised you have the gall to write the above line. Please wake up.
Your nap is now at least 14 years long.
{
int i; // Always need an 'i', ...
long val = 0x12345678;

X.lng = val;
for( i = 3; i >= 0; i-- )
{
printf( "%#02x\n", X.byt[i] );
}
Try the following on for size. Not that "the most recent store" was to the
'.lng' member, which is different from the '.byt' member.

[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.70) One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members.


} // main

--
Martin Ambuhl

Nov 13 '05 #6

P: n/a
> >#include <stdio.h>
union _x
{
long lng;
char byt[4];


Stop right here: what makes you think a long is four chars wide?


#include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #7

P: n/a
cody wrote:
#include <stdio.h>
union _x
{
long lng;
char byt[4];


Stop right here: what makes you think a long is four chars wide?


#include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?


Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation, and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...

... but other than that, Mrs. Lincoln, how did you
like the play?

--
Er*********@sun.com
Nov 13 '05 #8

P: n/a
> > > >I'm suprised no one suggested a union!
>
>#include <stdio.h>
> union _x
> {
> long lng;
> char byt[4];

Stop right here: what makes you think a long is four chars wide?


That could be easily overcome with sizeof. It's more that you can't rely on the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.

Isn't that what a union is good for???

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #9

P: n/a
In article <bm************@ID-176797.news.uni-berlin.de>,
"cody" <do*********************@gmx.de> wrote:
> >I'm suprised no one suggested a union!
> >
> >#include <stdio.h>
> > union _x
> > {
> > long lng;
> > char byt[4];
>
> Stop right here: what makes you think a long is four chars wide?

That could be easily overcome with sizeof. It's more that you can't rely on the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.

Isn't that what a union is good for???


Stay with freeware. Don't even think about getting a paid job as a
programmer.
Nov 13 '05 #10

P: n/a
"David Rubin" <bo***********@nomail.com> schrieb im Newsbeitrag
news:3F***************@nomail.com...
Serve La wrote:

"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:bk********************************@4ax.com...
>I'm suprised no one suggested a union!
>
>#include <stdio.h>
> union _x
> {
> long lng;
> char byt[4];

Stop right here: what makes you think a long is four chars wide?


That could be easily overcome with sizeof. It's more that you can't rely on the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.


With one exception, which is valid in this case:

[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.70)

<<< look here below! >>>
One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #11

P: n/a
Eric Sosman wrote:
cody wrote:
> >#include <stdio.h>
> > union _x
> > {
> > long lng;
> > char byt[4];
>
> Stop right here: what makes you think a long is four chars wide?


#include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?


Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation, and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...


For completeness, there's also a syntax error.

Jeremy.
Nov 13 '05 #12

P: n/a
> > #include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?
Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation,


What reserved identifier? Do you mean the underscore before x?
and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...
He just wanted to extract the bytes of a long. Sometimes you do not care how
the
bytes are represented are in that long, just getting them is enough.

You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.
... but other than that, Mrs. Lincoln, how did you
like the play?


What do you mean with that?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #13

P: n/a
> >> #include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?


Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation, and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...


For completeness, there's also a syntax error.

The missing semicolon at the end?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #14

P: n/a
"cody" <do*********************@gmx.de> wrote:
"David Rubin" <bo***********@nomail.com> schrieb im Newsbeitrag
news:3F***************@nomail.com...
Serve La wrote:
>
> "Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
> news:bk********************************@4ax.com...
> > >I'm suprised no one suggested a union!
> > >
> > >#include <stdio.h>
> > > union _x
> > > {
> > > long lng;
> > > char byt[4];
> >
> > Stop right here: what makes you think a long is four chars wide?
>
> That could be easily overcome with sizeof. It's more that you can't relyon > the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.


With one exception, which is valid in this case:


What makes you think so? Implementation defined means "not portable"
and there are no structs in the union, so why should ISO/IEC 9899:1999
6.5.2.3#5 be applicable in this case?

BTW, you should name version, chapter & verse if you quote a standard
(or a draft version of it, like you did).

<6.5.2.3#5 snipped>

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #15

P: n/a
"cody" <do*********************@gmx.de> wrote:
>> #include <stdio.h>
>> union _x
>> {
>> long lng;
>> char byt[sizeof long];
>> }
>>
>> better?
>
> Marginally. It still doesn't address the issue raised
> by both David Rubin and Martin Ambuhl, and it still uses
> an identifier reserved for the implementation, and it still
> risks trouble with trap representations or if the system
> uses signed magnitude or ones' complement arithmetic ...


For completeness, there's also a syntax error.


The missing semicolon at the end?


Actually, there are two syntax errors, of which the
missing semicolon is one.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #16

P: n/a
cody wrote:
>> #include <stdio.h>
>> union _x
>> {
>> long lng;
>> char byt[sizeof long];
>> }
>>
>> better?
>
> Marginally. It still doesn't address the issue raised
> by both David Rubin and Martin Ambuhl, and it still uses
> an identifier reserved for the implementation, and it still
> risks trouble with trap representations or if the system
> uses signed magnitude or ones' complement arithmetic ...


For completeness, there's also a syntax error.

The missing semicolon at the end?


That's one. There's another.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #17

P: n/a
cody wrote:
#include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?


Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation,


What reserved identifier? Do you mean the underscore before x?
and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...


He just wanted to extract the bytes of a long. Sometimes you do not care how
the
bytes are represented are in that long, just getting them is enough.

You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.
... but other than that, Mrs. Lincoln, how did you
like the play?


What do you mean with that?


Cody,

They might be picking on you now. I'll move tentatively to your side.

This snippet fixes your remaining syntax error (you'll see it easily)
but provides fodder for our detractors.

I contend, as you seem to, that accessing u.lng as components of u.byt[]
is what unions are all about. We could be wrong.

Accessing memory through (unsigned char *) seems to be guaranteed to
work. I demonstrate both methods below. I don't understand why the first
is wrong while the second is right. Duck! :-)

#include <stdio.h>

typedef unsigned long ulong;
typedef unsigned char uchar;

union {
ulong lng;
uchar byt[ sizeof (ulong) ];
} u;

int main(void) {
int i, j = sizeof (ulong);
uchar *ucr = (uchar *)&u.lng;
u.lng = 0x12345678;
for (i = 0; i < j; ++i)
printf(" 0x%02X", u.byt[i]);
puts("");
for (i = 0; i < j; ++i)
printf(" 0x%02X", ucr[i]);
return 0;
}

--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #18

P: n/a
On Thu, 16 Oct 2003 00:41:46 +0200
"cody" <do*********************@gmx.de> wrote:
#include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}

better?
Marginally. It still doesn't address the issue raised
by both David Rubin and Martin Ambuhl, and it still uses
an identifier reserved for the implementation,


What reserved identifier? Do you mean the underscore before x?


_x is reserved, yes. Unless you fully understand the rules as to which
identifiers are reserved in what scope you should avoid starting any
identifier with an underscore.
and it still
risks trouble with trap representations or if the system
uses signed magnitude or ones' complement arithmetic ...


He just wanted to extract the bytes of a long. Sometimes you do not
care how the
bytes are represented are in that long, just getting them is enough.

You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.


No it isn't. You have been told in another post that it is not.

<snip>
[Freeware, Games and Humor]


If the freeware is written by you in C then it is probably worth far
less than it costs.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #19

P: n/a
Joe Wright <jo********@earthlink.net> wrote:
cody wrote:
> > #include <stdio.h>
> > union _x
> > {
> > long lng;
> > char byt[sizeof long];
> > }
> >
> > better?
<snip>I contend, as you seem to, that accessing u.lng as components of u.byt[]
is what unions are all about. We could be wrong.


As I understand it, the main purpose of unions is to make it possible
to define objects of a "generic" type that can hold values of different
types, one at a time. It was not introduced to the language to have
a convenient way of interpreting the same data (the implementations
internal representation of a value) in different ways by writing to one
union member and reading from another.

Moral: you write a long, you read a long. ;-)

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #20

P: n/a
Joe Wright wrote:
.... snip ...
I contend, as you seem to, that accessing u.lng as components of
u.byt[] is what unions are all about. We could be wrong.
You are. The purpose is to conserve storage space when only one
of a variety of facets of the object needs to be recorded.

Accessing memory through (unsigned char *) seems to be guaranteed
to work. I demonstrate both methods below. I don't understand why
the first is wrong while the second is right. Duck! :-)


Again, this depends on the objective. If you want to examine all
the bits in the machine, including any padding or trap value bits,
yes. If you want to extract bits from the long value, no, because
they may well include bits extraneous to the value.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #21

P: n/a
"cody" <do*********************@gmx.de> wrote in message
news:bm************@ID-176797.news.uni-berlin.de...
You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.


At first I too made similar unfounded assertions here,
due to my ignorance, but I *listened* when shown that
I was wrong.

-Mike

Nov 13 '05 #22

P: n/a
bd
cody wrote:
> > >I'm suprised no one suggested a union!
> > >
> > >#include <stdio.h>
> > > union _x
> > > {
> > > long lng;
> > > char byt[4];
> >
> > Stop right here: what makes you think a long is four chars wide?
>
> That could be easily overcome with sizeof. It's more that you can't
> rely on > the byte order if you try to do it portably.


No, the problem is that you can't write into one union field and read
from another.

Isn't that what a union is good for???


They're intended as convenient ways of storing one out of several types in
one field. E.g., in a lexer or parser you might use something like:

struct token {
enum { TOKEN_FOO, TOKEN_BAR } type;
union {
struct token_foo_data foo;
struct token_bar_data bar;
} data;
};

Then, when processing the tokens, read the enum, and then read one of the
union fields based on the enum.
Nov 13 '05 #23

P: n/a
"Irrwahn Grausewitz" <ir*******@freenet.de> schrieb im Newsbeitrag
news:8c********************************@4ax.com...
"cody" <do*********************@gmx.de> wrote:
>> #include <stdio.h>
>> union _x
>> {
>> long lng;
>> char byt[sizeof long];
>> } [..] For completeness, there's also a syntax error.


The missing semicolon at the end?


Actually, there are two syntax errors, of which the
missing semicolon is one.

OK, OK..

sizeof ( long )

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #24

P: n/a
>>> #include <stdio.h>
union _x
{
long lng;
char byt[sizeof long];
}
You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.


No it isn't. You have been told in another post that it is not.

When both variables in the union starts at the same address, writing to one
and reading from the other is safe.

Note the special guarantee:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.68) One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #25

P: n/a

On Thu, 16 Oct 2003, cody wrote:

As you have been told several times before, *don't snip attributions!*
They exist so that we can tell who said what.
#include <stdio.h>
union _x
{
long lng;
char byt[sizeof (long)];
}; You should not assign new values to byt, that could cause trap
representations as you said, but reading is allowed and safe.
No it isn't. You have been told in another post that it is not.


When both variables in the union starts at the same address, writing
to one and reading from the other is safe.


What part of "No it isn't" don't you understand? I think [whoever
that was] was *very* clear, and you can't claim misunderstanding
here.
Note the special guarantee:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.
Boom. Implementation-defined. Sure, it's not undefined behavior,
but it's certainly not portable (which is what "safe" usually
connotes in the context of c.l.c).
68) One special guarantee is made in
order to simplify the use of unions: If a union contains ^^ several structures that share a common initial sequence (see ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members


Right. Note the special guarantee.

It's nice that you've learned where to find the draft standard;
now if only you'd read it more carefully...

[Oh, and re the "4 bytes to 3 bytes" thread, you're wrong over there
too. Structure padding between bit-field members is implementation-
defined, and the standard says nothing about compiler switches.]

-Arthur

Nov 13 '05 #26

P: n/a
cody wrote:

When both variables in the union starts at the same address, writing to one
and reading from the other is safe.
Nonsense. *Every* variable in a union starts at the same
address (every addressable variable, that is: bit-fields have
no addresses as far as C is concerned).
Note the special guarantee:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.68) One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members


The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.

Are you having trouble understanding the English in
which the Standard is written? You appear to be posting
from a .de address, which might indicate that your native
language is German. The great number of grammatical errors
in your written English makes me think you must be American,
but if not -- well, your English is better than my German,
but perhaps not equal to the task of deciphering Standardese.

--
Er*********@sun.com
Nov 13 '05 #27

P: n/a
"Eric Sosman" <Er*********@sun.com> schrieb im Newsbeitrag
news:3F***************@sun.com...
cody wrote:

When both variables in the union starts at the same address, writing to one and reading from the other is safe.
Nonsense. *Every* variable in a union starts at the same
address (every addressable variable, that is: bit-fields have
no addresses as far as C is concerned).


union
{
int a;
struct
{
int b;
int c;
} X;
}

Note that a and c have different starting points. Bitfield does have an
address, while the bits they contain have not.
Note the special guarantee:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.68) One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members


The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.


#include <stdio.h>
union X
{
struct { long lng} A;
struct { char byt[sizeof (long)] } B;
};

And now? Two structs with a common starting point.
Are you having trouble understanding the English in
which the Standard is written?
I try my best :)
You appear to be posting
from a .de address, which might indicate that your native
language is German.
That is correct, I'm german.
The great number of grammatical errors
in your written English makes me think you must be American,
but if not -- well, your English is better than my German,
I didn't know grammar of American and British is different.
but perhaps not equal to the task of deciphering Standardese.


Honestly, not a simple task. :)

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #28

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> schrieb im Newsbeitrag
news:Pi**********************************@unix50.a ndrew.cmu.edu...
[Oh, and re the "4 bytes to 3 bytes" thread, you're wrong over there
too. Structure padding between bit-field members is implementation-
defined, and the standard says nothing about compiler switches.]


Does that mean in standard C you have no control over structure padding?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #29

P: n/a

On Fri, 17 Oct 2003, cody wrote:

"Eric Sosman" <Er*********@sun.com> schrieb...
cody wrote:

When both variables in the union starts at the same address, writing
to one and reading from the other is safe.
Nonsense. *Every* variable in a union starts at the same
address (every addressable variable, that is: bit-fields have
no addresses as far as C is concerned).


union
{
int a;
struct
{
int b;
int c;
} X;

};
Note that a and c have different starting points.
Correct. Eric should have said, "Every *member* of a union starts
at the same address"; note that 'a' and 'X' have the same address.
Bitfield does have an
address, while the bits they contain have not.


Incorrect. One cannot take the address of a bit-field member.

The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.


#include <stdio.h>
union X
{
struct { long lng} A;
struct { char byt[sizeof (long)] } B;
};

And now? Two structs with a common starting point.


And a common initial sequence of length zero. So you can write
zero bytes to &some_x.A, and read zero bytes from &some_x.B,
portably.

union Example {
struct { long x; } A;
struct { long y; long z; } B;
} ex;

Union 'Example' contains two structures with a common initial
sequence of length sizeof(long) bytes. Thus, one may portably
write to ex.A.x and then immediately read from ex.B.y, and
expect to get the same value back.

The great number of grammatical errors
in your written English makes me think you must be American,
but if not -- well, your English is better than my German,


I didn't know grammar of American and British is different.


The fact that the great number of grammatical errors in your
written English made Eric think you must have been American,
makes me think Eric must be British. ;-)

-Arthur
Nov 13 '05 #30

P: n/a
On Fri, 17 Oct 2003 00:23:18 +0200, cody wrote:
"Eric Sosman" <Er*********@sun.com> schrieb im Newsbeitrag
news:3F***************@sun.com...
cody wrote:
>
> When both variables in the union starts at the same address, writing to one > and reading from the other is safe.


Nonsense. *Every* variable in a union starts at the same
address (every addressable variable, that is: bit-fields have
no addresses as far as C is concerned).


union
{
int a;
struct
{
int b;
int c;
} X;
}

Note that a and c have different starting points. Bitfield does have an
address, while the bits they contain have not.


'c' is not a "variable in a union". The two members of the union
are 'a' and 'X'.
> Note the special guarantee:
> [#5] With one exception, if the value of a member of a union
> object is used when the most recent store to the object was
> to a different member, the behavior is
> implementation-defined.68) One special guarantee is made in
> order to simplify the use of unions: If a union contains
> several structures that share a common initial sequence (see
> below), and if the union object currently contains one of
> these structures, it is permitted to inspect the common
> initial part of any of them anywhere that a declaration of
> the completed type of the union is visible. Two structures
> share a common initial sequence if corresponding members
> have compatible types (and, for bit-fields, the same widths)
> for a sequence of one or more initial members


The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.


#include <stdio.h>
union X
{
struct { long lng} A;
struct { char byt[sizeof (long)] } B;
};

And now? Two structs with a common starting point.


These two struct do not share a common initial sequence. The excerpt
from the standard above is talking about situations like this:

union U
{
struct {
int a;
int b;
char * p;
} A;
struct {
int x;
int y;
double d;
} B;
};

In both structs, the first few members are of the same type.
You may therefore do something like:

U.A.a = get_an_int();
put_an_int(U.B.x);
Are you having trouble understanding the English in
which the Standard is written?


I try my best :)
You appear to be posting
from a .de address, which might indicate that your native
language is German.


That is correct, I'm german.
The great number of grammatical errors
in your written English makes me think you must be American,
but if not -- well, your English is better than my German,


I didn't know grammar of American and British is different.


Jetzt weißt du. Aber ich glaube nicht, dass Eric von den
Grammatikunterschiede sprach, sondern dass Amerikaner zu
oft ihre eigene Muttersprache nur schlecht beherrschen.

Übrigens, ich bin Amerikaner, also darf ich sowas sagen. :-)
Nov 13 '05 #31

P: n/a
"cody" <do*********************@gmx.de> wrote:
<snip>
When both variables in the union starts at the same address, writing to one
and reading from the other is safe.
But only under very special circumstances, I already gave you a hint
in Message 6j********************************@4ax.com .

Draft N??? 6.5.2.3 is what you quote, but it suspect you didn't read
it thoroughly, try again, please:
Note the special guarantee:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.68) One special guarantee is made in ^^^^^^^^^^^^^^^^^^^^^^ order to simplify the use of unions: If a union contains ^^^^^^^^^^^^^^^^^^^^^^ several structures that share a common initial sequence (see ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ below), and if the union object currently contains one of ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ these structures, it is permitted to inspect the common ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ initial part of any of them anywhere that a declaration of ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members


Now, the question is: "Where are the structures in this thread?" :-P

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #32

P: n/a
"cody" <do*********************@gmx.de> wrote:
"Eric Sosman" <Er*********@sun.com> schrieb im Newsbeitrag
news:3F***************@sun.com...

<snip>
The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.


#include <stdio.h>
union X
{
struct { long lng} A;
struct { char byt[sizeof (long)] } B;
};

And now? Two structs with a common starting point.


But still they do not "share a common initial sequence." Period.

<snip>
but perhaps not equal to the task of deciphering Standardese.


Honestly, not a simple task. :)


True 'nough.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #33

P: n/a
Sheldon Simms <sh**********@yahoo.com> wrote:

<snip>
Jetzt weit du. Aber ich glaube nicht, dass Eric von den
Grammatikunterschiede sprach, sondern dass Amerikaner zu
oft ihre eigene Muttersprache nur schlecht beherrschen.

brigens, ich bin Amerikaner, also darf ich sowas sagen. :-)


Did you ever read de.comp.lang.c? If so, what is your impression
about the skills of Germans regarding their native language? ;-)

--
Irrwahn,
press all keys at once to continue.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #34

P: n/a
On Fri, 17 Oct 2003 01:36:19 +0200, Irrwahn Grausewitz wrote:
Sheldon Simms <sh**********@yahoo.com> wrote:

<snip>
Jetzt weißt du. Aber ich glaube nicht, dass Eric von den
Grammatikunterschiede sprach, sondern dass Amerikaner zu
oft ihre eigene Muttersprache nur schlecht beherrschen.

Übrigens, ich bin Amerikaner, also darf ich sowas sagen. :-)
Did you ever read de.comp.lang.c?


I did
If so, what is your impression
about the skills of Germans regarding their native language? ;-)


I was so enthralled by the never-ending fights over whether people
have .signatures of the proper length, or have a valid email address
in their headers, or why one shouldn't use a nickname when posting,
etc., that I didn't have time to consider the grammar.

-Sheldon

Nov 13 '05 #35

P: n/a

"cody" <do*********************@gmx.de> wrote in message
news:bm************@ID-176797.news.uni-berlin.de...
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> schrieb im Newsbeitrag
news:Pi**********************************@unix50.a ndrew.cmu.edu...
[Oh, and re the "4 bytes to 3 bytes" thread, you're wrong over there
too. Structure padding between bit-field members is implementation-
defined, and the standard says nothing about compiler switches.]


Does that mean in standard C you have no control over structure padding?


Correct. The only guarantee is that there is no padding
before the first member.

-Mike
Nov 13 '05 #36

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi***********************************@unix50. andrew.cmu.edu...
The fact that the great number of grammatical errors in your
written English made Eric think you must have been American,
makes me think Eric must be British. ;-)


I took it to be a remark about the sad state of the American
education system, with which I would agree.

-Mike
Nov 13 '05 #37

P: n/a
in comp.lang.c i read:
Does that mean in standard C you have no control over structure padding?


correct -- there is no standard mechanism available to control padding.

--
a signature
Nov 13 '05 #38

P: n/a
Sheldon Simms <sh**********@yahoo.com> wrote:
On Fri, 17 Oct 2003 01:36:19 +0200, Irrwahn Grausewitz wrote:

<snip>
Did you ever read de.comp.lang.c?


I did
If so, what is your impression
about the skills of Germans regarding their native language? ;-)


I was so enthralled by the never-ending fights over whether people
have .signatures of the proper length, or have a valid email address
in their headers, or why one shouldn't use a nickname when posting,
etc., that I didn't have time to consider the grammar.


In fact those fights are the very reason why I read d.c.l.c [1], because
I enjoy to have some odd twenty jokes delivered daily for free, albeit
grammatically flawed ones. ;-)

I still wonder why they did not rename the group to d.c.l.c.flames yet.

[1] I do not bother to post there. To those who think c.l.c can be a
rough place: try d.c.l.c.

Irrwahn
--
Inertia makes the world go around
Nov 13 '05 #39

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> schrieb im Newsbeitrag
news:Pi***********************************@unix50. andrew.cmu.edu...

On Fri, 17 Oct 2003, cody wrote:

Bitfield does have an
address, while the bits they contain have not.


Incorrect. One cannot take the address of a bit-field member.


Sorry thereis a misunderstanding in terminology here. With bitfield i meant
struct X{int a:4;b:6;}x; And with "bits" and meant the members of the
bitfield. So we both were right :)
The special guarantee applies only to union members
that happen to be structs, and only to structs if they
"share a common initial subsequence." The union you
exhibited contained no structs at all, and is not covered
by the special guarantee.


#include <stdio.h>
union X
{
struct { long lng} A;
struct { char byt[sizeof (long)] } B;
};

And now? Two structs with a common starting point.


And a common initial sequence of length zero. So you can write
zero bytes to &some_x.A, and read zero bytes from &some_x.B,
portably.

union Example {
struct { long x; } A;
struct { long y; long z; } B;
} ex;

Union 'Example' contains two structures with a common initial
sequence of length sizeof(long) bytes. Thus, one may portably
write to ex.A.x and then immediately read from ex.B.y, and
expect to get the same value back.


Does that apply to my struct too? Iam not really sure wheather long and
char[sizeof(long)] are compatible in this situation.
The great number of grammatical errors
in your written English makes me think you must be American,
but if not -- well, your English is better than my German,


I didn't know grammar of American and British is different.


The fact that the great number of grammatical errors in your
written English made Eric think you must have been American,
makes me think Eric must be British. ;-)


If you find a mistake in my postings please said whats wrong do that i can
do it better next time.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #40

P: n/a
"Sheldon Simms" <sh**********@yahoo.com> schrieb im Newsbeitrag
news:pa****************************@yahoo.com...
On Fri, 17 Oct 2003 01:36:19 +0200, Irrwahn Grausewitz wrote:
Sheldon Simms <sh**********@yahoo.com> wrote:

<snip>
Jetzt weit du. Aber ich glaube nicht, dass Eric von den
Grammatikunterschiede sprach, sondern dass Amerikaner zu
oft ihre eigene Muttersprache nur schlecht beherrschen.

brigens, ich bin Amerikaner, also darf ich sowas sagen. :-)


Did you ever read de.comp.lang.c?


I did
If so, what is your impression
about the skills of Germans regarding their native language? ;-)


I was so enthralled by the never-ending fights over whether people
have .signatures of the proper length, or have a valid email address
in their headers, or why one shouldn't use a nickname when posting,
etc., that I didn't have time to consider the grammar.

That's the reason why I as a german prefer to use english newsgroups instead
of german ones! There are so many stupid and irrelevant discussions about
proper capitalisation and realnames and so. Additionally I like english cos
I find it is more functional than german.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #41

P: n/a
On Fri, 17 Oct 2003 20:15:47 +0200, in comp.lang.c , "cody"
<do*********************@gmx.de> wrote:
> struct { long lng} A;
> struct { char byt[sizeof (long)] } B;
Does that apply to my struct too? Iam not really sure wheather long and
char[sizeof(long)] are compatible in this situation.
The answer is NO. Can you pass a char[] to a function expecting a
long? Can you pass a long to a function expecting a char? The standard
means what it says - the initial sequence of the two structs must be
identical in type.
If you find a mistake in my postings please said whats wrong do that i can
do it better next time.


Its "please say" rather than "please said". Said is the past tense.
Also please capitalise the letter i when used as the personal pronoun,
as I have done here.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #42

P: n/a
"Mark McIntyre" <ma**********@spamcop.net> schrieb im Newsbeitrag
news:je********************************@4ax.com...
On Fri, 17 Oct 2003 20:15:47 +0200, in comp.lang.c , "cody"
<do*********************@gmx.de> wrote:
> struct { long lng} A;
> struct { char byt[sizeof (long)] } B;
Does that apply to my struct too? Iam not really sure wheather long and
char[sizeof(long)] are compatible in this situation.


The answer is NO. Can you pass a char[] to a function expecting a
long? Can you pass a long to a function expecting a char? The standard
means what it says - the initial sequence of the two structs must be
identical in type.
If you find a mistake in my postings please said whats wrong do that i

cando it better next time.


Its "please say" rather than "please said". Said is the past tense.
Also please capitalise the letter i when used as the personal pronoun,
as I have done here.

Thx.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #43

P: n/a
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:christian.bau-201190.23210115102003@slb-> > > > Isn't that what a union
is good for???

Stay with freeware. Don't even think about getting a paid job as a
programmer.


It can be far worse. I work with somebody who didn't even know C has unions.
Nov 13 '05 #44

P: n/a
Serve La wrote:

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:christian.bau-201190.23210115102003@slb-> > > > Isn't that what a union
is good for???

Stay with freeware. Don't even think about getting a paid job as a
programmer.


It can be far worse. I work with somebody who didn't even know C has unions.


And dues-paying union members. :-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #45

P: n/a

"Serve La" <i@bleat.nospam.com> wrote in message
news:bm**********@news4.tilbu1.nb.home.nl...
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:christian.bau-201190.23210115102003@slb-> > > > Isn't that what a union is good for???

Stay with freeware. Don't even think about getting a paid job as a
programmer.


It can be far worse. I work with somebody who didn't even know C has

unions.

Hopefully he's not a member of a union as well. :-)

-Mike
Nov 13 '05 #46

This discussion thread is closed

Replies have been disabled for this discussion.