confusion in "union"

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?๐Ÿ˜•

Replies

  • silverscorpion
    silverscorpion
    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? ๐Ÿ˜€๐Ÿ˜€
  • samaira
    samaira
    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
  • pradeep_agrawal
    pradeep_agrawal
    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
  • skipper
    skipper
    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?)

You are reading an archived discussion.

Related Posts

hello guyz, I am rahul from faridabad. i am new here and i like this forum. thanks to the admin for creating such a good forum for future engineers. i...
Hi Ceans, Could you please say me basics of database testing? How can we test database efficiently? Is there is any specific methodology to test the database? Awaiting your replies...
Dear freinds, I have a very typical problem at home re electrical appliances.Brief description: When my radio is on AM station & at the same time I put on the...
Folks, I wish to add BSE/NSE ticker to a website. Can anyone help me with it? I may use the one provided by widgetbox; but I'd like to have one...
Hello, this may not be the proper forum for such a question, but I feel it may be the most appropriate place. I was wondering if anyone here knows of...