473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

union with packed struct

Hi all,
Here is a sample code segment:
....
typedef PACKED struct
{
union
{
PACKED struct
{
char red:1;
char green:1;
char blue:1;
} color_bit;
char color;
} color_u;
} color_t;

color_t color;
....
My question is: which bit in "color.color_u.color" reflect
"color.color_u.color_bit.red", "color.color_u.color_bit.green" and
"color.color_u.color_bit.blue"? Is this well defined?
In other words, if I set "color.color_u.color_bit.red" as 0,
"color.color_u.color_bit.green" as 0 and "color.color_u.color_bit.blue"
as 1, What will be "color.color_u.color"? is it 0x04?

Nov 14 '05
56 10355
On Wed, 9 Mar 2005 16:49:29 -0600, in comp.lang.c , "DHOLLINGSWORTH2"
<DH*************@cox.net> wrote:
All dynamic data is stored in memory, ram., no not ROM you cant load a
programm into rom, you burn a program into rom.
String literals are not dynamic data, and are explicitly allowed to be stored
anywhere. My computer's ROM contains the word Intel. My compiler, when
encountering a string literal "Intel" is allowed to point to that ROM address if
it wants.
and yes it is an int[],
No, and no amount of shouting and stamping your little foot, will make it so.
Go Blow Yourself!


Fool
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #51
On Wed, 9 Mar 2005 17:16:34 -0600, in comp.lang.c , "DHOLLINGSWORTH2"
<DH*************@cox.net> wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:i6********************************@4ax.com.. .
On Wed, 9 Mar 2005 02:43:27 -0600, in
Tell that to Intel. By the way, ever heard of a chip called the 8086?
Are you all Software nerds? Are do any of you know what HW is?


Lets put that comment back in context. You saidThere is still only one address per Location!
And if you have ever encountered x86 segmented architecture, you;ll know that to
be false. Its been a while, but I believe 0F00 and 00F0 are the same on that
arch.
And by the way, if you know *anything* about the Pentium, you'll be aware that
by using selectors you can point several addresses to the same location.
Why dont you let intel do the telling.
I am doing. Why not read your manual for x86 assembly programming?
Thats what i've done for nearly 20 years.


And yet you've never come across a machine with nonlinear memory. Interesting.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #52
On Wed, 9 Mar 2005 16:49:29 -0600, in comp.lang.c , "DHOLLINGSWORTH2"
<DH*************@cox.net> wrote:

All dynamic data is stored in memory, ram., no not ROM you cant load a
programm into rom, you burn a program into rom.


Oh, and by the way, is virtual memory in RAM? .
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #53
On Wed, 9 Mar 2005 17:34:35 -0600, in comp.lang.c , "DHOLLINGSWORTH2"
<DH*************@cox.net> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote
Who says the i86 architecture is the corner-stone of the computing
world? There are other processors, you know.


show me one,


you want to see other processors? Go look in a mac shop, or under your car
bonnet (hood if you're a yank), in your fridge, in your calculator, in your
computer (yup), etc etc. Sheesh, they're all over the place.
I know how to build ram in HW. Do you.


yeah, triff. I work with bond traders, so I know all about big swinging.....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #54
"DHOLLINGSWORTH2" <DH*************@cox.net> wrote:

[ Yet more nonsense, ending in: ]
Go Blow Yourself!
=----


Ok, let's look at the evidence, shall we?

- Posts provably untrue statements
- Insists that he's right even in the face of overwhelming evidence,
which would have made anyone merely wrong at least reconsider
- Has reverted to top-posting, and now has even
- stopped providing any context at all (yet is not on Google Broken)
- Refers to entirely ephemeral experience in merely related fields
- Tries to insult the competence of anyone who disagrees with him
- Finally, resorts to obscenities

Gentlemen, I'm not so sure we're dealing with a mere kook here. He
doesn't strike me as innocently imbecilic. I call him a troll.

Richard
Nov 14 '05 #55
Walter Roberson wrote:
Richard Bos wrote:

Equal, yes. You're allowed to compare pointers for
equality. This is simply done even in segmented or
type-separated memory by noting that two non-null
pointers of different type need never compare equal
at all:

Old Wolf wrote:
Not true. For example, pointers to different members of a
union must compare equal.


Only when "suitably converted".


I presumed that Richard's comment was before "suitable
conversion" in situations where no explicit conversion
is required.

Otherwise I don't see the value of what he is saying,
since it isn't possible to compare two non-null pointers
of different type (if you try to do so, and no constraint
violations occur, one of them will undergo an implicit conversion).

The only case I can see in C89 under which Richard was incorrect
was the clause about pointer to void.


Which cases is he correct about? Can you post some sample code,
if my above paragraph is unclear?

Nov 14 '05 #56
In article <11**********************@z14g2000cwz.googlegroups .com>,
Old Wolf <ol*****@inspire.net.nz> wrote:
:Walter Roberson wrote:
:> Richard Bos wrote:
:> Old Wolf wrote:
:> >Not true. For example, pointers to different members of a
:> >union must compare equal.

:> Only when "suitably converted".

:I presumed that Richard's comment was before "suitable
:conversion" in situations where no explicit conversion
:is required.

:Otherwise I don't see the value of what he is saying,
:since it isn't possible to compare two non-null pointers
:of different type (if you try to do so, and no constraint
:violations occur, one of them will undergo an implicit conversion).

His point was that there are constrain violations involved in all
such cases of comparing non-NULL pointers of different types.
And he was right except that he missed the void* case (which I
probably would have missed too.)
:Can you post some sample code,
:if my above paragraph is unclear?

Your wording implied that for any two union members, &u.i == &u.j
and my reply was that "You can't do that" unless the types are
compatable or you have undertaken a cast that might have no meaning.

In the code below, gcc and SGI's cc warn about every pointer comparison
except when comparing identical objects or when comparing void * to
another pointer.
I was thinking of chopping out the identical object case when I
generated the table; the result turned out to be interesting.
In SGI's cc, u.Float == u.Float and u.Double == u.Double but in
gcc, u.Float != u.Float and u.Double != u.Double . (These are
not pointer comparisons, they are comparisons of the result of
interpreting a pointer as float or double.) This might be chance
positioning on the stack, but I suspect more likely it is NaN checks,
since IEEE 754 defines NaN to not compare equal to itself.

#include <stdio.h>

#define CP(T1,T2) printf( "&u.%s %s &u.%s\n", #T1, \
&u.T1 == &u.T2 ? "==" : "!=", #T2 )
#define CP2(T1,T2) printf( "u.%s %s u.%s\n", #T1, \
u.T1 == u.T2 ? "==" : "!=", #T2 )
#define CP3(T1,T2) printf( "u.%s %s &u.%s\n", #T1, \
u.T1 == &u.T2 ? "==" : "!=", #T2 )

int main(void) {

union {
float Float;
double Double;
int Int;
long Long;
short Short;
char Char;
long long LongLong;
unsigned int UnsignedInt;
unsigned char UnsignedChar;
void* VoidPtr;
} u;

u.VoidPtr = (void *)&u;

CP(Float,Float);
CP(Double,Float);
CP(Double,Double);
CP(Int,Float);
CP(Int,Double);
CP(Int,Int);
CP(Long,Float);
CP(Long,Double);
CP(Long,Int);
CP(Long,Long);
CP(Short,Float);
CP(Short,Double);
CP(Short,Int);
CP(Short,Long);
CP(Short,Short);
CP(Char,Float);
CP(Char,Double);
CP(Char,Int);
CP(Char,Long);
CP(Char,Short);
CP(Char,Char);
CP(LongLong,Float);
CP(LongLong,Double);
CP(LongLong,Int);
CP(LongLong,Long);
CP(LongLong,Short);
CP(LongLong,Char);
CP(LongLong,LongLong);
CP(UnsignedInt,Float);
CP(UnsignedInt,Double);
CP(UnsignedInt,Int);
CP(UnsignedInt,Long);
CP(UnsignedInt,Short);
CP(UnsignedInt,Char);
CP(UnsignedInt,LongLong);
CP(UnsignedInt,UnsignedInt);
CP(UnsignedChar,Float);
CP(UnsignedChar,Double);
CP(UnsignedChar,Int);
CP(UnsignedChar,Long);
CP(UnsignedChar,Short);
CP(UnsignedChar,Char);
CP(UnsignedChar,LongLong);
CP(UnsignedChar,UnsignedInt);
CP(UnsignedChar,UnsignedChar);
CP(VoidPtr,Float);
CP(VoidPtr,Double);
CP(VoidPtr,Int);
CP(VoidPtr,Long);
CP(VoidPtr,Short);
CP(VoidPtr,Char);
CP(VoidPtr,LongLong);
CP(VoidPtr,UnsignedInt);
CP(VoidPtr,UnsignedChar);
CP(VoidPtr,VoidPtr);

CP2(Float,Float);
CP2(Double,Float);
CP2(Double,Double);
CP2(Int,Float);
CP2(Int,Double);
CP2(Int,Int);
CP2(Long,Float);
CP2(Long,Double);
CP2(Long,Int);
CP2(Long,Long);
CP2(Short,Float);
CP2(Short,Double);
CP2(Short,Int);
CP2(Short,Long);
CP2(Short,Short);
CP2(Char,Float);
CP2(Char,Double);
CP2(Char,Int);
CP2(Char,Long);
CP2(Char,Short);
CP2(Char,Char);
CP2(LongLong,Float);
CP2(LongLong,Double);
CP2(LongLong,Int);
CP2(LongLong,Long);
CP2(LongLong,Short);
CP2(LongLong,Char);
CP2(LongLong,LongLong);
CP2(UnsignedInt,Float);
CP2(UnsignedInt,Double);
CP2(UnsignedInt,Int);
CP2(UnsignedInt,Long);
CP2(UnsignedInt,Short);
CP2(UnsignedInt,Char);
CP2(UnsignedInt,LongLong);
CP2(UnsignedInt,UnsignedInt);
CP2(UnsignedChar,Float);
CP2(UnsignedChar,Double);
CP2(UnsignedChar,Int);
CP2(UnsignedChar,Long);
CP2(UnsignedChar,Short);
CP2(UnsignedChar,Char);
CP2(UnsignedChar,LongLong);
CP2(UnsignedChar,UnsignedInt);
CP2(UnsignedChar,UnsignedChar);

/*
Void* to a non-pointer won't pass the parser so skip that
and try pointer tests instead
*/

CP3(VoidPtr,Float);
CP3(VoidPtr,Double);
CP3(VoidPtr,Int);
CP3(VoidPtr,Long);
CP3(VoidPtr,Short);
CP3(VoidPtr,Char);
CP3(VoidPtr,LongLong);
CP3(VoidPtr,UnsignedInt);
CP3(VoidPtr,UnsignedChar);
CP3(VoidPtr,VoidPtr);
}
--
"[...] it's all part of one's right to be publicly stupid." -- Dave Smey
Nov 14 '05 #57

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Rasmussen | last post by:
I have some data packed in by bitfields: struct Data { unsigned long a : 8; unsigned long b : 14; unsigned long c : 7; unsigned long d : 3; };
3
by: Sid | last post by:
Hi folks, I wrote this code to test if I get the same address for all the variables in a union but for some reason the address I got when I used a char variable in a union seems bizarre- can...
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
10
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
3
by: parag.kanade | last post by:
I have a packet structure which has a field of Integer arrays, that is packed struct { int a; char b; int count; }foo; I need to initialize all the entries of count to a particular value,
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
3
by: Lighter | last post by:
Why does Platform SDK define union LARGE_INTEGER in such a way? ntdef.h defines the union LARGE_INTEGER as follows: typedef union _LARGE_INTEGER { struct { ULONG LowPart;
9
by: Sikandar | last post by:
Hi, I am beginner in C. Pls let me know what is packed array in C. Where is it used? Thanks, Sikandar
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.