Thread: Structures and unions

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    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??
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User TravisS's Avatar
    Join Date
    Jun 2002
    Posts
    536
    Well, if you have one variable you are using only for bit manipulation, I would certainly rather deal with bitfields

  7. #7
    Unregistered
    Guest
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-21-2008, 04:18 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 12:43 AM
  4. Unions and Structures
    By C-Struggler in forum C Programming
    Replies: 4
    Last Post: 03-06-2003, 11:28 AM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM