# Structures - Unions

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-25-2003
AProg
Structures - Unions
I am in the chapter "Structures, Unions, Bit Manipulations and Enumerations" in the book i read to learn the C language, and i

1)can't understand where i should use "typedef" and why not to use merely "struct".What's the difference?

2)I also don't undrerstand the difference between strustures and unions.

3)Also, in what cases should i use Bitwise operators?
• 05-25-2003
Magos
Re: Structures - Unions
Quote:

1)can't understand where i should use "typedef" and why not to use merely "struct".What's the difference?
It's a matter of taste. The typedef thingy removes the neccessicity (sp?) to put the keyword struct in front of every declaration of a structure. I find this useful to handle it as any other datatype.

Code:

```struct MyStruct1 {   int Var1;   int Var2; }; void MyFunc1(struct MyStruct1 Arg) {   ... } typedef struct {   int Var1;   int Var2; }MyStruct2; void MyFunc2(MyStruct2 Arg) {   ... }```
Quote:

2)I also don't undrerstand the difference between strustures and unions.
Complete difference. A structure is a collection of data (-types) while a union makes two datatypes share the same memory.

Quote:

3)Also, in what cases should i use Bitwise operators?
Depends. Can be used for flags among lots of things.
• 05-25-2003
Prelude
>1)can't understand where i should use "typedef" and why not to use merely "struct".What's the difference?
Using typedef on a struct allows you to use the typedef in declarations without the struct keyword:
Code:

```struct my_struct {   ... }; ... struct my_struct instance; /* Valid */ my_struct instance; /* Invalid */```
Code:

```typedef struct my_struct {   ... } my_struct; ... struct my_struct instance; /* Valid */ my_struct instance; /* Also valid */```
This is the only difference, you're using the typedef for readability purposes.

>2)I also don't undrerstand the difference between strustures and unions.
A structure is a heterogeneous collection of objects, the size of the structure is the size of the sum of the objects plus any padding between them.
Code:

```struct A {     int a;     double d;     char c; };```
struct A has an int and a double and a char. The size of an instance of A is sizeof (int) + sizeof (double) + sizeof (char) + any padding.

A union on the other hand, is also a collection of heterogeneous objects, but a union can only hold one of them at any given time. The size of the union is the size of its largest item.
Code:

```union B {     int a;     double d;     char c; };```
union B has an int or a double or a char. The size of B is sizeof (double).

>3)Also, in what cases should i use Bitwise operators?
Whenever you need to work with individual bits. There are many applications.
• 05-25-2003
AProg
???
So the only difference between structures and unions it's the way they get their size?

>...but a union can only hold one of them at any given time.
What do you mean? A union holds only a variable? If yes, then why to use a union and not ony a common variable?

Further explanation would be really appriciated.
• 05-25-2003
Prelude
>What do you mean?
In a union, there is only space for one member at a time. In other words, if you have an int and a double in a union, you can use the double but not the int, or vice versa. They both use the same location in memory to hold their values:
Code:

```union A {     int i;     double d; };```
An instance of A can have an int or a double, but not both at once.
• 05-25-2003
AProg
>An instance of A can have an int or a double, but not both at once.

I still can't get you.
How can i choose an int rather than a double?
• 05-25-2003
Prelude
>I still can't get you.
I can only explain this so many ways.

>How can i choose an int rather than a double?
Okay, you have this union
Code:

```union A {     int i;     double d; } a;```
If you say a.i = 10; then you can use a.i to access the value 10. If you want a double, you can then say a.d = 5.5; and be able to use 5.5 as a double from a.d, but you can no longer get 10 from a.i. If you suddenly decide you want 10 again, you must then say a.i = 10; one more time. Now you can use 10 as an int from a.i, but you can't get 5.5 from a.d. This is what I mean by one at a time. If you want a.i to have 10 and a.d to have 5.5 at the same time, you use a structure.
• 05-25-2003
Magos
While on the topic, what is the real use of a union? If you're having a real shortage of memory, sure a union would be a nice choice but with computers today this is most unlikely the case.
• 05-25-2003
Wledge
An example from "Expert C Programming"
Code:

```union bits32_tag {     int whole;     struct {char c0,c1,c2,c3;} byte; };```
This union allows the programmer to extract the full 32 Bit value,or the individual byte fields, value.byte.c0 and so on.
Without the need for extra assignment or typecast.
• 05-25-2003
stumon
Quote:

Originally posted by Magos
While on the topic, what is the real use of a union? If you're having a real shortage of memory, sure a union would be a nice choice but with computers today this is most unlikely the case.
Sure, computers these days have enough memory to handle just about any small program, so there is no major need for unions to save memory. But remember, there are still embedded systems that have very limited amounts of memory. There a union would be useful.
• 05-25-2003
Prelude
>If you're having a real shortage of memory, sure a union would be a nice
>choice but with computers today this is most unlikely the case.
In theory it may seem that saving memory isn't worth the effort these days, but you would be just as quick to complain about the detrimental effects of using a struct where a union would be better suited. We as programmers shouldn't be overly obsessed with efficiency, but when the most natural solution is also efficient, we should happily use it and be thankful of the savings.
• 05-25-2003
poccil
Unions are generally used to make structures that can assume more than one type, such as this example:

Code:

```typedef struct{  int datatype; // current type of the data  union{   char c;   int i;   long l;   float f;   double d;  }d; } MULTITYPESTRUCT;```
One use I recently saw is the use of a union to create a type useful for data alignment:

Code:

```typedef union{  long l;  float f;  double d;  long long ll; } MAXSIZETYPE; #define DATABLOCKSIZE sizeof(MAXSIZETYPE);```
• 05-25-2003
vikasgp
One use of unions is to use them as a "generic" type to hold one of many related struct's. For example, the GDK library(a graphics library on top of X) uses a union to hold structs for events(keyboard, mouse, etc).

Code:

```union GdkEvent {   GdkEventMotion            motion;   GdkEventButton            button;   GdkEventScroll            scroll;   GdkEventKey                    key;   ... }```
• 05-25-2003
AProg
That sounds interesting, vikasgp. Where can i find this library and what other functions does it have?
• 05-26-2003
quzah
Quote:

Originally posted by AProg
That sounds interesting, vikasgp. Where can i find this library and what other functions does it have?
If it takes you ten posts to understand what a union is, then there is no way on earth you're going to understand what the GDK libraries are doing.

Quzah.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last