1. ## Structs and Unions

Hi,

Now, what is the advantage of a union/struct???????
you can just declare a int if you want an int, float if you want a float and so on...
And a Union only can contain 1 (one) value, the largest variable.
So can someone tell me a scenario where a Union/Struct is very helpfull?

Encrypted

2. I don't use unions very often, but puttnig a group of similar variables together within a struct can be very useful. First of all, it allows you to keep track of variables within your program. The example that I usually give is that if you're trying to keep track of the number of screws you have by size, it makes sense to put those variables (half-inch, quarter-inch, etc.) in a box called "screws" so you don't confuse the number of screws with the number of bolts. This way you can have a screws.half-inch and a bolts.half-inch; everyone knows exactly what variable you're talking about just by looking at it, and there isn't any conflict.

The other argument for using a struct is the ability to typedef it to create your own complex datatypes. For example, each node in a linked list has to have at least one storage area (to hold the data), and one refference area (to point to the next node in the list). Using structs and typedefs you can make a handy node type that deals with that for you, and saves some code writing.

Code:
```typdedef struct Node
{
char cell[1];
void *next;
}```
We can create a new node simply be saying
Code:
`Node newnode;`
At least that's my understanding of it, but there may be some parts of the picture that I'm missing too.

Hope this helps,

starX
www.axisoftime.com

3. >Now, what is the advantage of a union/struct???????

A struct can be used to put related data together in an easy way. One application of structs is in parameter passing. Instead of passing large lists of parameters, you can pass a struct to a function. Another application of structs is in datastructures like lists, graphs and trees.

An application of a union can be mapping. For example mapping a variable on a bitfield or array. If you have a stream of bytes, then you can map it onto a structure.

4. Hi,
Thank you both for the replies,
and StarX, you seem to use structs in a different way i do, for example:
#include <stdio.h>
#include <string.h>

struct members
{
char name[15];
char sex;
int age;
}

main()
{
struct members smith;

strcpy( smith.name, "Smith, J" );
smith.sex= 'm';
smith.age= 20;

printf("\n\tName member:\t\t%s \n", smith.name );
printf("\tSex member:\t\t%c \n", smith.sex );
printf("\tAge member:\t\t%i \n", smith.age );

getche();
}
Anyway, i think i just found out some nice scenarios where structs are very helpfull.
But thanks for the help

Encrypted

5. Structures are good for holding lots of related information, such as all the information about a book in a library(name, author, price etc) and are comparable to records in other languages.

I have used unions a lot recently, they are good for (in this case) reading a text file of different length records into structures inside a union, so that the union reserves the memory for the largest record and makes the other records the same size so that you can (again in this case) read them into memory and qsort() them.