Thread: Structs and Unions

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    24

    Structs and Unions

    Hi,

    I just read this paper about unions and structs.
    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?

    Thank you in advance

    Encrypted

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    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. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >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. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    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. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions & Structs... Puzzled.
    By Sparrowhawk in forum C Programming
    Replies: 10
    Last Post: 12-14-2008, 04:45 PM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. unions and structs
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-24-2008, 10:54 PM
  4. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 09:46 AM
  5. why unions and structs couldnt be initialized ?
    By black in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2002, 05:05 AM