ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
uinon Person
{
int age_according_to_mother ;
int age_according_to_father ;
}
int age_according_to_mother = 10 ;
int age_according_to_father = 20 ;
printf("Age_according_to_Mother :: %d" , age_according_to_mother );
the result is :: 20 ; why ....
& now where is our declaration .... means , we have defined age_according_to_mother = 10 ; age_according_to_father = 20 ; means they both got the space in memory , but we have used union so they are overlapped , but if they are overlapped than how our program finds or sets the value of age_according_to_mother = 20 !
That's perfectly normal union behavior.
I guess you should make sure you got unions right...
but can you please explain this ,
the result is :: 20 ; why ....
Quote:
& now where is our declaration .... means , we have defined age_according_to_mother = 10 ; age_according_to_father = 20 ; means they both got the space in memory , but we have used union so they are overlapped , but if they are overlapped than how our program finds or sets the value of age_according_to_mother = 20 ! how our program finds age_according_to_mother
Last edited by tushar_pandey; 08-24-2012 at 03:36 AM.
So, then the answer is right here . I mean u_1.age1's value is printed as 20 here. whenever, you use the union, it is allocated with the memory space of its only one member(of largest size)
EX: union a{ int b; double c;};
Here union 'a' is allocated with 8bytes (Bcoz highest bytesized member is 'double' and is of 8bytes).
And a basic fact of union is that it can hold the value of a single member at any instant. And also all the members of union are given the same address (i.e beginning address of the union itself). I hope this is enough for you to crack the question
Convinced????????
And a basic fact of union is that it can hold the value of a single member at any instant. And also all the members of union are given the same address (i.e beginning address of the union itself). I hope this is enough for you to crack the question
Strict? Maybe. I just suggested you to read again what unions are and how they work because your "issue" was, likely, a misunderstanding on the subject. I was going to explain a bit more, but SIG_SEV came first
Regarding the "dangerous" bit, I don't know how one can be dangerous by pointing someone else to read again its documentation...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.