Structures - Unions

This is a discussion on Structures - Unions within the C Programming forums, part of the General Programming Boards category; I am in the chapter "Structures, Unions, Bit Manipulations and Enumerations" in the book i read to learn the C ...

  1. #1
    Banned
    Join Date
    May 2003
    Posts
    124

    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?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Structures - Unions

    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)
    {
       ...
    }
    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.

    3)Also, in what cases should i use Bitwise operators?
    Depends. Can be used for flags among lots of things.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  4. #4
    Banned
    Join Date
    May 2003
    Posts
    124

    ???

    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  6. #6
    Banned
    Join Date
    May 2003
    Posts
    124
    >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?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    148
    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.

  10. #10
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    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.
    The keyboard is the standard device used to cause computer errors!

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    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);

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    19
    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;
       ...
    }

  14. #14
    Banned
    Join Date
    May 2003
    Posts
    124
    That sounds interesting, vikasgp. Where can i find this library and what other functions does it have?

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

Page 1 of 2 12 LastLast
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, 01:57 PM
  3. Unions and Structures
    By C-Struggler in forum C Programming
    Replies: 4
    Last Post: 03-06-2003, 10:28 AM
  4. Structures and unions
    By Paninaro in forum C Programming
    Replies: 6
    Last Post: 06-21-2002, 01:35 PM
  5. Replies: 7
    Last Post: 12-29-2001, 10:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21