Thread: Problem in Union Array program

  1. #1
    Registered User
    Join Date
    May 2011

    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?

    #include <stdio.h>
    #include <stdlib.h>
    int main()
        union salary
            char name[20];
            int age, bas, da, hra, tot;
        } s[2];
        int i;
            printf("Enter Name");
            printf("\n Enter Age");
            printf("\n Enter Basic Salary");
        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;
            printf("\n %d",a+1);
            printf("\t %s",s[a].name);
            printf("\t %d",s[a].age);
            printf("\t %d",s[a].bas);
            printf("\t %d",s[a].da);
            printf("\t %d",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
    The edge of the known universe
    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.
    union foo {
      char name[100];
      int age;
    } foovar;
    strcpy(, "hello" );
    printf("%s\n", );
    // do NOT attempt to use foovar.age
    // Then later on
    foovar.age = 42;
    printf("%d\n", foovar.age );
    // do NOT attempt to use
    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
    Join Date
    Aug 2010
    Ontario Canada
    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
    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, 03:39 PM
  2. Help With Union-Find Problem
    By Bnchs in forum C++ Programming
    Replies: 0
    Last Post: 12-02-2007, 08: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

Website Security Test