if union could...

This is a discussion on if union could... within the C++ Programming forums, part of the General Programming Boards category; Hi all~ if union could supply only int and float data ? I tried some other types but no correct ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy if union could...

    Hi all~

    if union could supply only int and float data ?

    I tried some other types but no correct response for me.
    Never end on learning~

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    union can supply more than int and float:
    Code:
    #include <limits.h>
    #include <iostream.h>
    
    union UNION
    {
      short s;
      unsigned int i;
      char ch;
      long l;
    
      union
      {
        char c;
        unsigned char uc;
      };
    };
    
    int main()
    {
      UNION u;
    
      u.ch = 'a';
      cout << "ch        = " << u.ch << endl;
    	
      u.s = SHRT_MAX;
      cout << "MAX SHORT = " << u.s << endl;
    
      u.i = UINT_MAX;
      cout << "MAX UNIT  = " << u.i << endl;
    
      u.l = LONG_MAX;
      cout << "MAX LONG  = " << u.l << endl;
    
      u.uc = UCHAR_MAX;
      cout << "MAX CHAR  = " << (int)u.uc << endl;
    
      return 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if union could supply only int and float data ?
    A union can supply any data type, but only one at a time.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just curious, Prelude. Why do you always end your posts/replies with -Prelude?
    I mean, no one can be so lazy they can't turn their head 5 degrees to the left and see it there?

    (Just curious, that's all. No offense intended)
    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.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Arrow

    Originally posted by Prelude
    >if union could supply only int and float data ?
    A union can supply any data type, but only one at a time.

    -Prelude
    any data type ? I tried string but got an error.
    Never end on learning~

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    a struct or typedef struct might suit your needs better if your using strings
    What is C++?

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Post

    Originally posted by Vicious
    a struct or typedef struct might suit your needs better if your using strings
    Mmm...........then what is the advantage of union please ?
    Never end on learning~

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    lol, dunno...

    i might not even be right about that
    What is C++?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >any data type ? I tried string but got an error.
    I'm sorry, I thought I had said "almost any type". Some class objects will cause problems with the memory handling of unions, such as the string class. Built in types are guaranteed, but user defined data types can be invalid because of overloaded operators, constructor features, etc...

    >Why do you always end your posts/replies with -Prelude?
    Two reasons: First, I got into the habit and never got out of it. Second, in case I get logged out while responding with a lengthy post, the readers will still know who I was. It's all about ego.

    >then what is the advantage of union please ?
    They are a way of avoiding bloated code when space is at a premium. Most of the time you won't use them, but it's nice to have the option of optimizing for space when you need it.

    -Prelude
    Last edited by Prelude; 06-13-2002 at 08:37 AM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 08:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 09:12 AM

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