Thread: wrong output in struct cum union example

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    wrong output in struct cum union example

    Hi!

    I am getting unexpected output in the following code:

    Code:
    #include<conio.h>
    #include<stdio.h>
    using namespace std;
     struct A {
                    union {
                            int age;
                        
                            char name[35];
                    };
            };
    
    
    int main(int argc, char* argv[])
    {
    
    	 A a;
    
    	 cout<<"Enter name";
    
      	 gets(a.name);
    
    	 int l=strlen(a.name);
    	 cout<<l<<"\n";
    	 
         cout<<"Enter age";
    	 cin>>a.age;
    
    	 cout<<"\n";
    
    	 puts(a.name);
    
    	 cout<<"\t"<<a.age;
    
    	 getch();
    	return 0;
    }
    This program display correct age but wrong name... why???

    Since both are the members of union and I am accessing both of them with structure

    object.....Age is display correct but name display garbage...

    Can anybody explain me reason and solution....

    thanks
    Bhagwat

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You might want to look up what a union actually is. Try to think of it as an OR. You can either look at the age OR you can look at the name. You can't use both at the same time in your example. If you change age, you also change the first few bytes of name. They share a certain amount of memory. Perhaps you wanted a structure rather than a union?
    Last edited by SKeane; 01-10-2007 at 05:10 AM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably you want
    Code:
    struct A 
    {
       int age;
       char name[35];
    };
    PS. If you program in C - remove "using namespace std;" and cout
    If you program in C++ - go to the correct forum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    Thanks

    Thanks to both of u !

    Now I have understand what the problem was...

    Thanks a lot once again.

    Bhagwat

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > gets(a.name);
    Never EVER use gets() again!

    See the FAQ.
    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.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    It was C++ code on C board and with out using the iostream and non standard header conio.h

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 10:12 AM