CrazyEngineers
  • confusion in "union"

    samaira

    Member

    Updated: Oct 14, 2024
    Views: 996
    UNION uses just one location (max among all the menbers of the UNION) but then say my UNION has int and float initially I read a int value it gets stored in a particular location. Now when I read the float value I lose my int value because i will be overwriting the int value.
    so my question is where does the previous value get stored?😕
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • silverscorpion

    MemberAug 14, 2009

    Well, I think there's no harm in 'reading' a union value.

    If your union has an int and a float, the memory space allocated will only be for a float, which is 4 bytes, let's say.


    So, if you write an int in this space, it'll get stored in the first two bytes of the 4 byte segment.
    If you now read the float value, the value stored in all four bytes will be read. That does nothing to the first two bytes, so that int value will be intact.

    For ex, consider this

    union{
    int a; float b; } sample.

    sample.a=100;
    printf("%d", sample.b);
    Here, you're just reading the float value. So, after this, if you print the value of a, it'll still be 100;
    It's only when you alter the float value that the int value also gets changed. If you do something like this,

    sample.a=100;
    sample.b++;
    printf("%d", sample.a);
    Now the output wont be 100.
    Clear? 😀😀
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberAug 14, 2009

    thik hai but then whats the use??usually union is preferred due to its less memory allocation as compared to structure but if modification can change the value of other variable which i might be needed in future then how to get back
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberAug 14, 2009

    In union memory is shared between different variables which can be used for:
    - Manipulation of value of variable of one type by manipulation variable of other type.
    - In scenario where the data type to be used can be different based on user input and using union in this case reduces the memory usage.

    For detail on how memory is shared refer below post:

    #-Link-Snipped-#

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • skipper

    MemberAug 14, 2009

    It's actually the same thing as in set theory. You map two or more structures (abstract) to the same physical values - machine-wise. So your program needs to determine which structure the values represent.

    In C, there is no reason you can't read an int, and print a char; or the other way around. Typing is a mechanism that lets you select values according to "type". The machine is independent of data types, it just has registers, (you know, with binary strings in them).
    😀

    There's more to it than saving space: it lets you overload. Overloading is important for some reason (I wonder what it is?)
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register