Data structures question

S

Shwetabh

Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance
 
E

Eric

Shwetabh said:
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Well, depending on what I need to do, I will use which ever one is most
appropriate.

They are not interchangeable, but since you seem to be implying that
they are, I am wondering if you really understand the differences
between them.
 
E

Emmanuel Delahaye

Shwetabh wrote on 26/07/05 :
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

I don't see what you meant. Just implement the data structures in the
way that seems to be the best for your application. If you want an
union, 'just do it'...


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
J

John Bode

Shwetabh said:
Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance

Unions are not more "efficient" than structs, any more than sizeof is
more efficient than strlen(); they are different entities, with
different purposes. You can't replace one with the other.
 
A

Alex Fraser

John Bode said:
Unions are not more "efficient" than structs, any more than sizeof is
more efficient than strlen(); they are different entities, with
different purposes. You can't replace one with the other.

Consider:

struct foo {
enum foo_type t; /* specifies which member of u is valid */
union {
int i;
unsigned u;
/* ... */
} u;
};

In this case, with what I hope is obvious use, you can replace the union
with a struct (without affecting correctness), but doing so increases the
size of struct foo making it less memory efficient.

Alex
 
L

Lawrence Kirby

Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance

A union only allows you have have data stored in one of its members at any
particular time. When that is what you need you can use a union. However
most datastructures require data to be stored in more than one member at
ther same time so a union would not work and you would use a structure for
those.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top