Thread: Problem in Union Array program

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Post Problem in Union Array program

    I created my first union program and now I am not able to complete it. It shows no error.

    In outpur window, I cant see the name, there is random symbol at place of name.

    HRA and total is showing wrong.

    Any hint?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        union salary
        { 
            char name[20];
            int age, bas, da, hra, tot;
        } s[2];
        int i;
        for(i=0;i<2;i++)
        {
            printf("Enter Name");
            scanf("%s",s[i].name);
            printf("\n Enter Age");
            scanf("%d",&s[i].age);
            printf("\n Enter Basic Salary");
            scanf("%d",&s[i].bas);
            
        }
        printf("\n \t \t \t \t ABC LTD");
        printf("\n \t \t \t \t Jaipur");
        printf("\n -------------------------");
        printf("\n Salary Sheet");
        printf("\n -------------------------");
        printf("\n S. no \t Name \t Age \t Basic \t DA \t HRA \t Total");
        
        int a;
        for(a=0;a<2;a++)
        {
            printf("\n %d",a+1);
            printf("\t %s",s[a].name);
            printf("\t %d",s[a].age);
            printf("\t %d",s[a].bas);
            s[a].da=s[a].bas*50/100;
            printf("\t %d",s[a].da);
            s[a].hra=s[a].bas*10/100;
            printf("\t %d",s[a].hra);
            s[a].tot=s[a].bas+s[a].da+s[a].hra;
            printf("\t %d",s[a].tot);
        }
        printf("\n ------------------------");
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    That's because you need to be using a struct, not a union - if you want to store ALL the members.

    A union stores only ONE member at once.
    Code:
    union foo {
      char name[100];
      int age;
    } foovar;
    
    strcpy( foovar.name, "hello" );
    printf("%s\n", foovar.name );
    // do NOT attempt to use foovar.age
    
    // Then later on
    foovar.age = 42;
    printf("%d\n", foovar.age );
    // do NOT attempt to use foovar.name
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What you need in this case is a struct, not a union.

    In a struct your variables are stored one after another and thus can keep separate values.
    In a union your variables are piled one on top of the other and cannot keep separate values.

    Simply change the word union to the word struct... you,ll see the difference right away.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Wooo
    worked. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing bit fields using a union problem.
    By samdomville in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 04:39 PM
  2. Help With Union-Find Problem
    By Bnchs in forum C++ Programming
    Replies: 0
    Last Post: 12-02-2007, 09:17 AM
  3. Problem with using a union into a struct :)
    By g_p in forum C Programming
    Replies: 11
    Last Post: 05-12-2007, 03:21 PM
  4. dynamic union array doesn't work
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 05-08-2005, 08:46 AM
  5. Problem with 'union REGS in, out;'
    By JLBSchreck in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2003, 12:57 AM

Tags for this Thread