unions: multiple inputs (second input overwrites first?)

Posted on 2004-11-17
Last Modified: 2010-04-15
Hi, i'm doing a project for my class and can't seem to figure this out.

The first part is pretty simple. We simply had to write a program in C with a union with these conditions

union numType
      float f;
      unsigned int ui;
union numType u;

and input a number as a float and print out the binary in 32bit representation of it as an unsigned integer using a decimal to binary converter function. However, we had to split the binary up for the signed bit, exponent (excess 127), and the fractional part or mantisa.

Now, the second part where I have a problem. We have to get the user to input 2 short integers and display them in 32bit binary representation just like the first part. In addition, we also have to do it for 2 unsigned short ints and an array of 4 characters in 32bit. So using the union:

union numType
      short int s1,s2;
      unsigned short int us1,us2;
      char arry[3];
      unsigned char arryc[3];      

I understand how to do this and it's supposed to be simple right? However, when I get the user to input 2 short integers the second one overwrites the first one. I'm going about this with a basic:

      printf("Please enter 2 short integer numbers, 0 to exit ");

am I doing something wrong here, why can't I input both values into the union? our teacher even said that is how you setup the union, but there must be something I'm doing wrong with the way i'm trying to input the values?
Question by:zennx
    1 Comment
    LVL 45

    Accepted Solution

    Hi zennx,

    Your union should be

    union numType
         short int s[2];
         unsigned short int us[2];
         char arry[4];
         unsigned char arryc[4];    

    I hope this should enumerate what you were doing wrong

    1. arr[3] is an array of 3 elements and not 4 ... You need 4 characters
    2. Concept of union is that storage corresponding to largest elements is assigned and reading in a different element will overwrite the previous one ... Since you need to have two shorts at one instance, it is imperative that you have enough storage in the union to store 2 shorts at the same time or you can use a dirtier method and have atleast a component which can hold two shorts and do the packing and unpacking into that element on your own.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now