LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2012, 02:42 AM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
Union :: problem with declaration .


Code:
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 !
 
Old 08-24-2012, 03:15 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
How can you even get said code to compile?
  • union is misspelled (uinon);
  • the union declaration is not semicolon-terminated
  • the two int variables don't even refer to the union fields
You should post a meaningful compilable portion of your code that's behaving strangely, as it seems you can compile it.
 
1 members found this post helpful.
Old 08-24-2012, 03:19 AM   #3
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
Put semicolon after the union definition..........
 
Old 08-24-2012, 03:29 AM   #4
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Code:
#include<stdio.h>

typedef union Person
{
        int age_according_to_mother ;
        int age_according_to_father ;
}person ;

int main()
{
        person u_1 ;

        u_1.age_according_to_mother = 10 ;
        u_1.age_according_to_father = 20 ;

        printf("%d",u_1.age_according_to_mother);

        printf("\n");
        return 0 ;
}

Last edited by tushar_pandey; 08-24-2012 at 03:32 AM.
 
Old 08-24-2012, 03:32 AM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
That's perfectly normal union behavior.
I guess you should make sure you got unions right...
 
1 members found this post helpful.
Old 08-24-2012, 03:34 AM   #6
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
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.
 
Old 08-24-2012, 03:40 AM   #7
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
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????????
 
1 members found this post helpful.
Old 08-24-2012, 03:43 AM   #8
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
thanks ,

Quote:
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

it works ...
 
Old 08-24-2012, 03:53 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
If the issue is no more, mark the thread as solved, then.
 
Old 08-24-2012, 04:30 AM   #10
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
414N , you seems like strict + dangerous guy !
 
Old 08-24-2012, 06:09 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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...
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Implicit Declaration Problem Fritz_Doll Programming 2 03-23-2011 06:11 PM
A declaration problem Asuralm Programming 2 12-06-2007 11:32 AM
Strange problem with SLAX live cd 'union fs' -=Graz=- Slackware 0 04-29-2006 05:01 AM
union wait problem zaichik Programming 3 12-22-2004 09:59 AM
Problem with function declaration Linh Programming 3 04-26-2004 04:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration