Hi!
I am getting unexpected output in the following code:
This program display correct age but wrong name... why???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; }
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