# Structures and unions

• 06-19-2002
Paninaro
Structures and unions
I have studied structures and unions, but I still dont get what they are good for in a REAL programm, not in those cheesy examples that books come along with and which seem extremely complicated for what they really do.

Shall somebody explain??
• 06-19-2002
sean345
One use of structures is to return multiple values from a function. For example if I wanted to return a char*, int, and double from a function I could do this:
Code:

```    typedef structure TEST{     char * Name;     int Age;     double Money; } TEST; TEST Function(){     TEST Test;     Test.Age = 10;     strcpy(Name, "Courtney");     Money = 100.0;     return Test; }```
Eventually you will be able to make linked lists which come in very handy later on.

- Sean
• 06-19-2002
quzah
Taken a step further, in a multitude of other programming languages, they use the concept of 'Objects'. Consider:
Code:

```struct car {     short int model;     short int doors;     short int tires;     short int convertable;     short int fuel;     struct motor engine;  };```
And so on. The point is, why would I want to have a ton of single variables in my program? Suppose I want five cars. Do I want to have to make fifteen variables for EACH CAR? Hell no:

tires1, tires2, tires3, tires4 ...

See the problem?

Organization. That's why people use structures.

Quzah.
• 06-20-2002
Shiro
Unions can be used for a lot of things, you can use it for example to access individual bits within a bitstring. You create a union with a bitfield and for example an int in it. Then you can easily access the individual bits. Another example is when you want to have one datatype to use, but you want it to be used for different purposes which require different datatypes. You could define all datatypes within the enum, so that you can access hem using the union.
• 06-21-2002
MrWizard
Quote:

Originally posted by Shiro
Unions can be used for a lot of things, you can use it for example to access individual bits within a bitstring. You create a union with a bitfield and for example an int in it. Then you can easily access the individual bits. Another example is when you want to have one datatype to use, but you want it to be used for different purposes which require different datatypes. You could define all datatypes within the enum, so that you can access hem using the union.
If you wanted to access individual bits in an arbitrary data file, would it not be easier to simply use bitwise operators?
• 06-21-2002
TravisS
Well, if you have one variable you are using only for bit manipulation, I would certainly rather deal with bitfields ;)
• 06-21-2002
Unregistered
Quote:

I have studied structures and unions, but I still dont get what they are good for in a REAL programm, not in those cheesy examples that books come along with and which seem extremely complicated for what they really do.

Shall somebody explain??
Actually, structures are an amazingly usefull tool for programming.

The most practical example I can think of is computing the cross product of vectors. Basically, this is an operation that takes two vectors (we can assume the vectors are three dimensional), and results in a third vector. This is a very real example I assure you. While it might not come up in every program, things like this do come up, and we need to be able to handle them. It goes something like this...
Code:

```typedef struct {  double x, y, z; } DoubleVector;```
Code:

```DoubleVector crossProductDoubleVector (DoubleVector A, DoubleVector B) {  DoubleVector C;  C.x = A.y * B.z - A.z * B.y;  C.y = A.z * B.x - A.x * B.z;  C.z = A.x * B.y - A.y * B.x;  return C; }```
Code:

```// Function without structures... // Each vector has to be represented as 3 doubles void crossProcuctDoubleVector2 (double Ax, double Ay, double Az, double Bx, double By, double Bz, double * Cx, double * Cy, double * Cz) {  *Cx = Ay * Bz - Az * By;  *Cy = Az * Bx - Ax * Bz;  *Cz = Ax * By - Ay * Bx;  return; }```
Now, the two really aren't all that different, although the parameters of the second one are getting to be pretty long there. It's not so bad writing one function like that, especially one so simple as this, but as they get more complicated, you want to keep your variable declarations down, the more you have, the less readable it gets.

I have here a snippet of code that relies on the DoubleVector structure. I am pretty sure this is a good example of a situation where not using structures would force the programmer to worry too much about little details, like keeping track of 16 different variables that only represent about 3 or 4 different objects.

viewer points to a doublevector representing the viewer's position
screen points to a doublevector represenging a point through which the sight ray passes
points is an array of three doublevectors representing points on the plane that we are trying to find the distance to.
Code:

```double getPlane (DoubleVector const * viewer, DoubleVector const * screen, DoubleVector const * points) {         DoubleVector A =         {                 points[1].x - points[0].x,                 points[1].y - points[0].y,                 points[1].z - points[0].z,         };         DoubleVector B =         {                 points[2].x - points[0].x,                 points[2].y - points[0].y,                 points[2].z - points[0].z,         };         DoubleVector Normal = crossProductDoubleVector (&A, &B);         double D = dotProductDoubleVector (&points[0], &Normal);                 double top = dotProductDoubleVector (&Normal, viewer);         double bottom = top - dotProductDoubleVector (&Normal, screen);         top -= D;         return top / bottom; }```