473,326 Members | 2,655 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,326 software developers and data experts.

union access

typedef struct ntt {
int type;
union {
int i;
char* s;
};
}nt;

nt n;
n.i = 0;

I found a C example like this and could not get gcc 2.95.4 to compile it
(struct has no member named `i') until I declared an instance of the union:
union {
int i;
char* s;
}u;

and accessed like:
n.u.i = 0;

Is the first example valid? If so, where is the problem?

--
Sean Dolan
Nov 14 '05
73 3921
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ci*********@news3.newsguy.com...

In article <Zh******************@newssvr21.news.prodigy.com >, "Mabden" <mabden@sbc_global.net> writes:

No. Duh. I have never seen a union used to save memory.


Duh indeed. What makes you think that your personal experience is
definitive?


Because I have experienced it.

*I* have certainly seen unions used to save memory; I may have used
them that way myself, though it's a matter of interpretation. (If I
use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)
No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.
Note that even on systems with ample memory, a union may often win
over a structure with redundant members because of the limited size
of cache lines, among other things.
Which may result in a union inside an array. We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.

The main reason to use unions is to take in chunk of data in binary form and break it down into meaningful data. Things like packets that come in as X number of bits, and pulling out the real data as flags and bytes.
The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?


Yes, me.
We do this all the time when communicating with the mainframe.


Well hooray for you. Those of us who have to write *portable* C
comms code don't like to make assumptions about how implementations
will lay things out in memory, so we don't use unions for type
punning; we marshall and unmarshall packets manually, using unsigned
char, in the manner actually supported by the language.


Are you sure you shouldn't be in the C++ newsgroup?

You could just as well say that functions and arrays are the same thing because they both have curly braces to begin and end them.


This is as bogus an analogy as I have seen here in some time. unions
and structs are obviously more similar than functions and arrays,
since the former pair are both data structures; and arrays don't
"begin and end" with "curly braces" (though array initializers do).


Actually, I meant functions and structures. Sorry for the confusion.

--
Mabden
Nov 14 '05 #51
Mabden <mabden@sbc_global.net> wrote:
I didn't mean an array of unions. I said a union can't be used as an
array. I don't think you can say: union interface {
int input;
int ouput;
} u [50]; can you?


This *is* an array of unions, and yes, it is perfectly valid.

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #52
"Alex Monjushko" <mo*******@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...
Mabden <mabden@sbc_global.net> wrote:
I didn't mean an array of unions. I said a union can't be used as an
array. I don't think you can say:

union interface {
int input;
int ouput;
} u [50];

can you?


This *is* an array of unions, and yes, it is perfectly valid.


Oh.

Nevermind.
Nov 14 '05 #53
"Mabden" <mabden@sbc_global.net> writes:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
Mabden wrote: [...]
> Structs and unions have a similar definition, so the Standards
> Whores think they are the same, but in fact if they were that
> similar you could, for instance make a union array. That doesn't
> make sense, so that is one difference unions and structures have.
Except it does make sense, and arrays of unions are implemented
all the time. Your inexperience is showing again.


I didn't mean an array of unions. I said a union can't be used as an
array.


No, a union can't be used as an array (and I don't recall anyone
claiming otherwise). I suppose a struct can be used as an array:

struct {
int elem0;
int elem1;
int elem2;
/* etc. */
}

but it seldom makes much sense to do so. If you want an array, use an
array. If you want a union, use a union. If you want a struct, use a
struct.
I don't think you can say:
union interface {
int input;
int ouput;
} u [50];

can you?


Certainly you can. It's an array of unions. It defines a type "union
interface", and defines u as an array of 50 of them.

Here's a small program using the above declaration:

#include <stdio.h>
#include <stddef.h>

int main(void)
{
union interface {
int input;
int ouput;
} u [50];

printf("sizeof(int) = %d\n", (int)sizeof(int));
printf("sizeof(union interface) = %d\n",
(int)sizeof(union interface));
printf("offsetof(union interface, input) = %d\n",
(int)offsetof(union interface, input));
printf("offsetof(union interface, ouput) = %d\n",
(int)offsetof(union interface, ouput));
printf("sizeof u = %d\n", (int)sizeof u);
return 0;
}

And here's the output I got:

sizeof(int) = 4
sizeof(union interface) = 4
offsetof(union interface, input) = 0
offsetof(union interface, ouput) = 0
sizeof u = 200

The sizes will vary, of course, on systems where sizeof(int) != 4.

It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.

On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.
There is a certain irony in insulting people when it turns out that
they're right and you're wrong. But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).

Think before you post.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #54
On Wed, 22 Sep 2004 22:48:21 GMT
"Mabden" <mabden@sbc_global.net> wrote:
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ci*********@news3.newsguy.com...

In article <Zh******************@newssvr21.news.prodigy.com >,
"Mabden" <mabden@sbc_global.net> writes:

No. Duh. I have never seen a union used to save memory.


Duh indeed. What makes you think that your personal experience is
definitive?


Because I have experienced it.


That merely defines your experience and has nothing to do with what
occurs in the rest of the world.
*I* have certainly seen unions used to save memory; I may have used
them that way myself, though it's a matter of interpretation. (If I
use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)


No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.


I doubt that he is aiming at job security through obscurity. It is a
method I have seen used many times, including a lot of code written by
experienced programmers using the Pascal equivalent (variant records)
for projects having to meet MoD quality standards.
Note that even on systems with ample memory, a union may often win
over a structure with redundant members because of the limited size
of cache lines, among other things.


Which may result in a union inside an array.


Not a problem.
We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.


Well, it was you who just raised arrays of unions, not Michael.
The main reason to use unions is to take in chunk of data in
binary form and break it down into meaningful data. Things like packets that come in as X number of bits, and pulling out the real data as flags and bytes.

The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?


Yes, me.


You are only representative of you and I somehow doubt that you have
done most types of programming.
We do this all the time when communicating with the mainframe.


Well hooray for you. Those of us who have to write *portable* C
comms code don't like to make assumptions about how implementations
will lay things out in memory, so we don't use unions for type
punning; we marshall and unmarshall packets manually, using unsigned
char, in the manner actually supported by the language.


Are you sure you shouldn't be in the C++ newsgroup?


Why? What he said is true for C. I can't comment about whether it is
also true for C++, although I would suspect that it is.
You could just as well say that functions and arrays are the same thing because they both have curly braces to begin and end them.


This is as bogus an analogy as I have seen here in some time.
unions and structs are obviously more similar than functions and
arrays, since the former pair are both data structures; and arrays
don't"begin and end" with "curly braces" (though array initializers
do).


Actually, I meant functions and structures. Sorry for the confusion.


Unions and structures are obviously more similar that functions and
structures. For a start unions and structures are types of data storage,
functions are not.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #55
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mabden" <mabden@sbc_global.net> writes:
but it seldom makes much sense to do so. If you want an array, use an
array. If you want a union, use a union. If you want a struct, use a
struct.
I don't think you can say:
union interface {
int input;
int output;
} u [50];

can you?
Certainly you can. It's an array of unions. It defines a type "union
interface", and defines u as an array of 50 of them.

Here's a small program using the above declaration:


<snip>

It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.
Well, that _was_ actually the argument. Someone had a problem with Dan
Pop saying they were "completely different" and off it went. I think
they are completely different in use and concept, if not in declaration
or whatever the Standard Readers suggest. You don't usually use one
where the other is more apropos, but then the Standards Suitors got
their backs up...
On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.
Well, I was asking a question, not posting "facts".

There is a certain irony in insulting people when it turns out that
they're right and you're wrong.
Can I be wrong by asking a question? Am I wrong now?

But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).
And since they're not being paid, should I just say Standards Sluts? ;-)
<gd&r>
Are you a little over-sensitive about my phrasing? Do you consider
yourself to be included in this group, or are you just speaking up for
the maligned?
Think before you post.


Point taken, I sometimes get annoyed with the "believers" who spout
"nonsense"^w "the truth" about C99 when I don't have any idea how to get
a compiler that will comply to it...
Last note: are unions and structs the same in YOUR mind, Keith?

Nov 14 '05 #56
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:6f************@brenda.flash-gordon.me.uk...
On Wed, 22 Sep 2004 22:48:21 GMT
"Mabden" <mabden@sbc_global.net> wrote:
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ci*********@news3.newsguy.com...

In article <Zh******************@newssvr21.news.prodigy.com >,
"Mabden" <mabden@sbc_global.net> writes:
>
> No. Duh. I have never seen a union used to save memory.

Duh indeed. What makes you think that your personal experience is
definitive?


Because I have experienced it.


That merely defines your experience and has nothing to do with what
occurs in the rest of the world.

Oh.
*I* have certainly seen unions used to save memory; I may have used them that way myself, though it's a matter of interpretation. (If I use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)


No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.


I doubt that he is aiming at job security through obscurity. It is a
method I have seen used many times, including a lot of code written by
experienced programmers using the Pascal equivalent (variant records)
for projects having to meet MoD quality standards.

Pascal is not as versatile as C. You might have to do tricks to play
games around the compiler. Perhaps you should have used a Real language
to begin with...?
> You could just as well say that functions and arrays are the same
thing
> because they both have curly braces to begin and end them.

This is as bogus an analogy as I have seen here in some time.
unions and structs are obviously more similar than functions and
arrays, since the former pair are both data structures; and arrays
don't"begin and end" with "curly braces" (though array

initializers do).


Actually, I meant functions and structures. Sorry for the confusion.


Unions and structures are obviously more similar that functions and
structures. For a start unions and structures are types of data

storage, functions are not.


"more similar that functions" doesn't make sense. What I said does. See,
we are comparing "apples to oranges", so I am bound to compare things
that are dissimilar (is that a word? unsimilar?). See, I believe unions
and struct are dissimilar - even though they look the same. Is a duck a
goose? Is a zebra a mule?

--
Mabden


Nov 14 '05 #57
"Mabden" <mabden@sbc_global.net> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... [...]
It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.


Well, that _was_ actually the argument. Someone had a problem with Dan
Pop saying they were "completely different" and off it went. I think
they are completely different in use and concept, if not in declaration
or whatever the Standard Readers suggest. You don't usually use one
where the other is more apropos, but then the Standards Suitors got
their backs up...


No, that was actually not the argument. I'll try one more time to
explain the distinction.

Logically, there are three possibilities:

(1) Structures and unions are completely different.

(2) Structures and unions are similar in some ways, but significantly
different in other ways.

(3) Structures and unions are the same thing.

Dan Pop asserted (1). I disagreed, and asserted (2). You've been
arguing that the rest of are wrong for asserting (3), but in fact,
nobody has done so.
On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.


Well, I was asking a question, not posting "facts".


You've done both.
But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).


And since they're not being paid, should I just say Standards Sluts? ;-)
<gd&r>


No, you should keep your stupid insults to yourself. (Yes, I saw the
smiley.)
Are you a little over-sensitive about my phrasing? Do you consider
yourself to be included in this group, or are you just speaking up for
the maligned?
No, I am not being over-sensitive. You referred to some set of people
as "Standards Whores". It seemed clear from the context that I was
being included in that generalization. Calling people whores is
insulting. Taking offense at being called whores is not
"over-sensitive".

Based on your history, I suspect you didn't realize how offensive you
were being, and I'm not sure it's going to sink in even now (though
I'm prepared to be pleasantly surprised).
Think before you post.


Point taken, I sometimes get annoyed with the "believers" who spout
"nonsense"^w "the truth" about C99 when I don't have any idea how to get
a compiler that will comply to it...


I don't know what "nonsense" you're referring to in this context. In
particular, I think all the issues in this thread are common to C90
and C99.
Last note: are unions and structs the same in YOUR mind, Keith?


No, they are not the same. I have never said that they are, nor has
anyone else. I have clearly and repeatedly said that unions and
structs are two different things that are similar in some ways.
Please pay attention.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #58

In article <Vi******************@newssvr29.news.prodigy.com >, "Mabden" <mabden@sbc_global.net> writes:
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ci*********@news3.newsguy.com...
In article <Zh******************@newssvr21.news.prodigy.com >, "Mabden"

<mabden@sbc_global.net> writes:

No. Duh. I have never seen a union used to save memory.


Duh indeed. What makes you think that your personal experience is
definitive?


Because I have experienced it.


And you are ... megalomaniacal? schizophrenic? simply delusional?

Why is it that so many people on Usenet think that their experience
can be generalized to the whole world? Is it a problem of education,
or are they actually inherently stupid?

Y'know, Dan's attitude becomes more understandable every day.
*I* have certainly seen unions used to save memory; I may have used
them that way myself, though it's a matter of interpretation. (If I
use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)


No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.


Any developer who's confused by this very common use of unions
should not be maintaining C code, mine or anyone else's. I'm not
sure they should be doing any programming at all.
Note that even on systems with ample memory, a union may often win
over a structure with redundant members because of the limited size
of cache lines, among other things.


Which may result in a union inside an array. We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.


No, *I* am talking about unions, what they are commonly used for,
and why. I don't know what the hell *you* are talking about. It
doesn't appear to be C.
The main reason [more inanity].


The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?


Yes, me.


Well, I suppose if your group is "crap C programmers who don't know
how unions are commonly used, or what you can do with them", then
you may be representative. Purely by accident, but I suppose that's
how you get a lot of things done.
Well hooray for you. Those of us who have to write *portable* C
comms code don't like to make assumptions about how implementations
will lay things out in memory, so we don't use unions for type
punning; we marshall and unmarshall packets manually, using unsigned
char, in the manner actually supported by the language.


Are you sure you shouldn't be in the C++ newsgroup?


Nothing I wrote above has anything to do with C++, as distinct from
C. It's equally applicable to both. What reference to C++ did you
hallucinate?

--
Michael Wojcik mi************@microfocus.com

Reversible CA's are -automorphisms- on shift spaces. It is a notorious
fact in symbolic dynamics that describing such things on a shift of finite
type are -fiendishly- difficult. -- Chris Hillman
Nov 14 '05 #59
Dan Pop wrote:
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin
<ke**@hpl.hp.com> writes:

The difference is just that writing into one member makes the other
member's values undefined, which in turn is why overlapping them
makes sense.
Which is precisely what makes unions and structures completely different
things, despite any *superficial* similarities.


Just because there's an important difference doesn't make them
"completely different".

If structures and unions are "completely different", what are you
left with to express, say, the difference between ints and structures?
Unions and functions? Types and statements?
And of course they're syntactically almost identical.

Which means exactly zilch. if and while are also syntactically almost
identical, yet they are still completely different things.


You must have some awsomely picky notion of sameness, then, since
both if [1] and while are semantically very similar things -


You must have some awsomely lax notion of sameness, if the if and while
statements look semantically very similar to you. To me, the only
statements semantically close to while are do and for.


I don't weight the presence or absence of iteration as highly as you
do, and I rate the similarity of treatment of subcomponents more than
you do. Why is why I pointed out you could improve your argument by
picking `switch` instead of `if`.
Then again, a wheel mouse and a wheelbarrow might be semantically very
similar things to you...


They're much more similar than, say, a wheel mouse and a photon, or
a wheelbarrow and _Twelveth Night_, or a house and being arrested,
or a headache and a telephone.

[Just because I'm different from you doesn't mean I'm *completely*
different from you.]

--
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 14 '05 #60
"Mabden" <mabden@sbc_global.net> wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.
Well, that _was_ actually the argument. Someone had a problem with Dan
Pop saying they were "completely different" and off it went. I think
they are completely different in use and concept, if not in declaration
or whatever the Standard Readers suggest.


And they're _not_ completely different. Neither are they completely
equal. They are quite similar to some degree, and quite dissimilar in
other aspects. Such a subtle nuance may be too difficult to grasp for
black-and-white minds like yours and Dan Pop's, but that doesn't make it
any less valid.
On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.


Well, I was asking a question, not posting "facts".


You were _stating_, not asking but claiming outright, that
Structs and unions have a similar definition, so the Standards Whores
think they are the same, but in fact if they were that similar you
could, for instance make a union array.


And that is a nonsensical statement, as well as quite a stupid kind of
insult.
But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).


And since they're not being paid, should I just say Standards Sluts? ;-)


You, sir, are an imbecile.

Richard
Nov 14 '05 #61
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin <ke**@hpl.hp.com> writes:
[Just because I'm different from you doesn't mean I'm *completely*
different from you.]


OK, name two completely different things and explain why they are
completely different. I'm sure someone else would disagree ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #62
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
This whole argument is about a claim that structs and unions are
"completely different". They are neither identical nor completely
different; they're similar in some ways, different in others.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is true for *any* pair of things. By your logic, there are no two
things that are completely different.

By my logic, the huge semantic differences between unions and structures
(structure types are aggregate types, union types aren't) make them
completely different things.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #63
Dan Pop wrote:
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin
<ke**@hpl.hp.com> writes:
[Just because I'm different from you doesn't mean I'm *completely*
different from you.]


OK, name two completely different things and explain why they are
completely different. I'm sure someone else would disagree ;-)


Since they would share the property "named by Chris as an example",
the act of producing them would make them less different than they
were before.

Since you're pushing this side-track, am I to assume you acknowledge
that structs and unions are not *completely* different? I'm willing
to limit the kinds of properties under discussion to those relevant
to programming languages - "completely" comes with context, after
all.

--
Chris "" 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 14 '05 #64
Richard Bos wrote:
"Mabden" <mabden@sbc_global.net> wrote:
"Keith Thompson" <ks***@mib.org> wrote in message

.... snip ...
But there are really two separate
issues here. You should be more careful of your facts (whether
you're calling people "whores" or not), and you shouldn't call
people "whores" (whether you're right about the facts or not).


And since they're not being paid, should I just say Standards
Sluts? ;-)


You, sir, are an imbecile.


Don't credit him with that much intelligence and savoir faire.

--
"It is not a question of staying the course, but of changing
the course" - John Kerry, 2004-09-20
"Ask any boat owner the eventual result of continuing the
present course indefinitely" - C.B. Falconer, 2004-09-20

Nov 14 '05 #65
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin <ke**@hpl.hp.com> writes:
Since you're pushing this side-track, am I to assume you acknowledge
that structs and unions are not *completely* different?
Nope. Merely pointing out that is possible to define "completely
different" in such a way as to make it devoid of any practical meaning.
I'm willing
to limit the kinds of properties under discussion to those relevant
to programming languages - "completely" comes with context, after
all.


And I have *explicitly* limited the relevant properties to the semantic
ones. Syntactic similarities have no relevance in context.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #66
Dan Pop <Da*****@cern.ch> wrote:
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
This whole argument is about a claim that structs and unions are
"completely different". They are neither identical nor completely
different; they're similar in some ways, different in others.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is true for *any* pair of things. By your logic, there are no two
things that are completely different.

By my logic, the huge semantic differences between unions and structures
(structure types are aggregate types, union types aren't) make them
completely different things.


The degree of similarity between two objects is determined by the
set of their common attributes. As long as there are common attributes,
they can not be declared "completely different." As long as there are
different attributes, they can not be declared "exactly the same."

Furthermore, the degree of similarity between structs and unions happens
to be quite high, because they share more common attributes with each
other than with any other constructs in C. A significant semantic
difference does not render them "completely different."

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #67
Da*****@cern.ch (Dan Pop) writes:
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin
<ke**@hpl.hp.com> writes:
Since you're pushing this side-track, am I to assume you acknowledge
that structs and unions are not *completely* different?


Nope. Merely pointing out that is possible to define "completely
different" in such a way as to make it devoid of any practical meaning.
I'm willing
to limit the kinds of properties under discussion to those relevant
to programming languages - "completely" comes with context, after
all.


And I have *explicitly* limited the relevant properties to the semantic
ones. Syntactic similarities have no relevance in context.


We all know what structures are. We all (presumably) know what unions
are. The current debate about whether or not they're "completely
different" isn't about structures and unions; it's about the meaning
of the phrase "completely different". Perhaps alt.english.usage would
be a good place to have that debate, unless someone raises a point
that's actually about the C language.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #68
Keith Thompson wrote:
We all know what structures are. We all (presumably) know what unions
are. The current debate about whether or not they're "completely
different" isn't about structures and unions; it's about the meaning
of the phrase "completely different". Perhaps alt.english.usage would
be a good place to have that debate, unless someone raises a point
that's actually about the C language.


alt.usage.english has much better traffic and availability.


Brian Rodenborn
Nov 14 '05 #69
On Thu, 23 Sep 2004 00:28:37 GMT
"Mabden" <mabden@sbc_global.net> wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:6f************@brenda.flash-gordon.me.uk...
On Wed, 22 Sep 2004 22:48:21 GMT
"Mabden" <mabden@sbc_global.net> wrote:
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ci*********@news3.newsguy.com...
>
> In article <Zh******************@newssvr21.news.prodigy.com >,
> "Mabden"
<mabden@sbc_global.net> writes:
<snip>
> *I* have certainly seen unions used to save memory; I may have used > them that way myself, though it's a matter of interpretation.
> (If I > use a union as a container for various types of data, only one
> of which will be stored in it, for reasons of elegance and
> efficiency rather than to "save memory" per se, does that
> count?)

No. That is just to confuse the next guy that has to interpret
your code. Are you aiming for job security through obfuscation?
That never works.
I doubt that he is aiming at job security through obscurity. It is a
method I have seen used many times, including a lot of code written
by experienced programmers using the Pascal equivalent (variant
records) for projects having to meet MoD quality standards.


Pascal is not as versatile as C. You might have to do tricks to play
games around the compiler. Perhaps you should have used a Real
language to begin with...?


Your ignorance is showing again (I know complete embedded systems
including as much of an OS as was required that are implemented in
Pascal). Variant records are no more a"trick" than unions in C. In fact,
untagged variant records have identical almost semantics, right down to
the problems with writing as one type and reading as another.

Some things are actually easier in Pascal than they are in C! However, I
choose to use C rather than Pascal when given a choice.
> > You could just as well say that functions and arrays are the same thing
> > because they both have curly braces to begin and end them.
>
> This is as bogus an analogy as I have seen here in some time.
> unions and structs are obviously more similar than functions and
> arrays, since the former pair are both data structures; and
> arrays don't"begin and end" with "curly braces" (though array initializers > do).

Actually, I meant functions and structures. Sorry for the
confusion.


Unions and structures are obviously more similar that functions and
structures. For a start unions and structures are types of data

storage,
functions are not.


"more similar that functions" doesn't make sense.


So I made a typo. I would have thought that anyone could see than I
intended to say "than" rather than "that".
What I said does.
See, we are comparing "apples to oranges", so I am bound to compare
things that are dissimilar (is that a word? unsimilar?). See, I
believe unions and struct are dissimilar - even though they look the
same. Is a duck a goose? Is a zebra a mule?


To use your analogy, which things have more in common:
1) A zebra and a mule
2) A zebra and an apple.

Just to help you along I will point out that not only are zebras and
mules animals (unlike apples) with 4 legs and hoofs, both can be ridden
etc, but they also both have a common ancestor.

Then, just to help you a bit further, determining how similar two things
are is done by examining the two items to determine what characteristics
they have in common and to what degree and what aspects are different
and to what degree.

If one stick to black and white you don't need the word similar at all,
since either two things are exactly the same thing or they are different
things. So if you compare two mules you will find they are different (if
nothing else they are in different places and that is a difference)
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #70
"Default User" <fi********@boeing.com.invalid> writes:
Keith Thompson wrote:
We all know what structures are. We all (presumably) know what unions
are. The current debate about whether or not they're "completely
different" isn't about structures and unions; it's about the meaning
of the phrase "completely different". Perhaps alt.english.usage would
be a good place to have that debate, unless someone raises a point
that's actually about the C language.


alt.usage.english has much better traffic and availability.


Oops. Thanks for the correction.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #71
Flash Gordon wrote:
Your ignorance is showing again (I know complete embedded systems
including as much of an OS as was required that are implemented in
Pascal). Variant records are no more a"trick" than unions in C.
In fact, untagged variant records have identical almost semantics,
right down to
the problems with writing as one type and reading as another.


Here's what K&R2 says about unions:
6.8 Unions
They are analogous to variant records in Pascal.

--
pete
Nov 14 '05 #72
Dan Pop wrote:
In <ci**********@news.unix-apps.rit.sc2.udc.hpl.hp.com> Chris Dollin
<ke**@hpl.hp.com> writes:
Since you're pushing this side-track, am I to assume you acknowledge
that structs and unions are not *completely* different?


Nope. Merely pointing out that is possible to define "completely
different" in such a way as to make it devoid of any practical meaning.


Certainly, but I haven't done that. You can if you like, but it will make
the discussion a bit pointless.
I'm willing
to limit the kinds of properties under discussion to those relevant
to programming languages - "completely" comes with context, after
all.


And I have *explicitly* limited the relevant properties to the semantic
ones. Syntactic similarities have no relevance in context.


I just wanted to seal off an exit I thought you might have been forced
to take. I'm happy for you to have thrown away the key yourself.

--
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 14 '05 #73
Alex Monjushko wrote:
...
The degree of similarity between two objects is determined by the
set of their common attributes. As long as there are common attributes,
they can not be declared "completely different." As long as there are
different attributes, they can not be declared "exactly the same."
Yeah, please nobody tell that windows and linux or c and java are
completely different. As long as there are common attributes, they
cannot be declared "completely different".
Furthermore, the degree of similarity between structs and unions happens
to be quite high, because they share more common attributes with each
other than with any other constructs in C. A significant semantic
difference does not render them "completely different."


Yep. If you say that two things are "completely different", you better
talk to your lawyer first. And if somebody ask you what's the difference
between char and char*, you don't tell that they are different - first
explain everything you know about computer memory, blah-blah-blah, and
only after that you can make a remark (but don't insist) about semantic
difference.
Nov 14 '05 #74

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

Similar topics

3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
4
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly...
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
15
by: Ken Allen | last post by:
I have some code from C/C++ that I am attempting to port to C#. I have come across an interesting problem that is quite common in complex C/C++ code: the us of UNION in structure definitions to...
2
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified)...
16
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c)...
5
by: BillCo | last post by:
I've encountered a problem while using ADO to save query objects. Union queries created normally (via the interface) appear in adox catelog.procedures rather than catelog.views. This is reasonably...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
32
by: =?gb2312?B?zfWzrLey?= | last post by:
Union un { int I; char c; } main() { union un x; x.c=10; x.c=1;
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.