union problems return

This is a discussion on union problems return within the C++ Programming forums, part of the General Programming Boards category; ok let say i have a union called "shareLD" which takes this form: Code: union shareLD { double dval; long ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    union problems return

    ok let say i have a union called "shareLD" which takes this form:

    Code:
    union shareLD
    {
       double dval;
       long lval;
    };
    
    shareLD myUnion;
    myUnion.dval = 3.5;
    so now dval contains 3.5...i get that part...but does lval contain 3.0 since they share the same memory?

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>but does lval contain 3.0 since they share the same memory?
    No, you can only use one member at a time in a union.
    *Cela*

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You can use both members at same time. The problem here is that double is not an int-type. If you had for example the following union:

    Code:
    union myunion
    {
        int a;
        int b;
    };
    Now if you have a variable of type myunion and assign a value x to variable a, then b has the same value as a. They share the same memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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