Thread: Unions?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    Unions?

    What i Gotta DO:

    Your solution must include a function called displayUnion() which will take a integer_union variable as a parameter. The function will have simply 4 printf statements, and nothing else! This function will need to be invoked 4 times from the main() program.

    Create a union called "integer_union" with members char c, short s, int i and long l. Each union variable should be printed as a char, a short, an int and a long.

    char: w
    short: 1234
    int: 1234567
    long: 1234567890

    What i got:
    Code:
    #include<stdio.h>
    
    union integer_union{
    	char  c;
    	short s;
    	int   i;
    	long  l;
    };
    int main()
    {
    	union integer_union value;
    	
    	
    	printf( "Please enter a char: " );
    	scanf( "%c", &value.c, "%hd", value.s, "%d", &value.i, "%ld", &value.l );
    	printf( "char  c = %c\nshort s = %hd\nint   i = %d\nlong  l = %ld", value.c, value.s, value.i, value.l );
    
    	printf( "\n\nPlease enter a short: ");
    	scanf( "%hd", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	printf( "char  c = %c\nshort s = %hd\nint   i = %d\nlong  l = %ld", value.c, value.s, value.i, value.l );
    	 
    	printf( "\n\nPlease enter a int: ");
    	scanf( "%d", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	printf( "char  c = %c\nshort s = %hd\nint   i = %d\nlong  l = %ld", value.c, value.s, value.i, value.l );
    	
    	printf( "\n\nPlease enter a long: ");
    	scanf( "%ld", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	printf( "char  c = %c\nshort s = %hd\nint   i = %d\nlong  l = %ld\n\n", value.c, value.s, value.i, value.l );
    	
    	return 0;
    
    }

    It works but im not sure if this is the right way , not sure bout the function called displayUnion() how to implement it er what?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    If i do something like this it messes up
    Code:
    #include <stdio.h>
    
    
    void display_union();
    
    union integer_union {
    	char c;
    	short s;
    	int i;
    	long l;
    };
    
    int main()
    {
    	char c;
    	short s;
    	int i;
    	long l;
    
    	union integer_union integer_union;
    
    	printf( "please enter a char: " );
    	scanf( "%c", &c );
    
    		integer_union.c = c;
    		display_union();
    
    	printf( "Please enter a short: " );
    	scanf( "%hd", &s );
    
    		integer_union.s = s;
    		display_union();
    
    	printf( "Please enter an int: " );
    	scanf( "%d", &i );
    
    		integer_union.i = i;
    		display_union();
    
    	printf( "Please enter a long: " );
    	scanf( "%ld", &l );
    
    		integer_union.l = l;
    		display_union();
    
    	return 0;
    
    }
    
    void display_union( char c, short s, int i, long l )
    {
    	printf( "char c = %c\n", c );
    	printf( "short s = %hd\n", s );
    	printf( "int i = %i\n", i );
    	printf( "long l = %ld\n\n", l );
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to pass a structure to a function? You pass unions in the same manner. What it sounds like from your description is that you want a function that will be called like so:
    Code:
    union foo
    {
        ...stuff...
    };
    
    union foo bar; /* an instance of the type union foo */
    
    ...do something with bar...
    
    myfunction( bar );
    I'll leave that for you to think on. It's an easy implementation. Post back if you're still stuck with what you've tried.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    well still dont understand ... no diff in codes.. damn it .. brain hurts

  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
    > If i do something like this it messes up
    Here's your prototype
    void display_union();

    Here's your definition
    void display_union( char c, short s, int i, long l )

    Spot the difference.

    If your compiler doesn't complain, add more warning levels or get a better compiler.
    If your compiler does complain, start paying attention to what it says.

    Here's my warnings
    Code:
    $ gcc -W -Wall -ansi -pedantic foo.c
    foo.c:51: error: conflicting types for 'display_union'
    foo.c:51: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    foo.c:4: error: previous declaration of 'display_union' was here
    foo.c:51: error: conflicting types for 'display_union'
    foo.c:51: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    foo.c:4: error: previous declaration of 'display_union' was here
    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
    Join Date
    Jun 2005
    Posts
    24
    Hows this... it works
    Code:
    #include <stdio.h>
    
    
    void display_union(char c, short s, int i, long l);
    
    union integer_union {
    	char c;
    	short s;
    	int i;
    	long l;
    };
    
    int main()
    {
    	union integer_union value;
    
    	printf( "Please enter a char: " );
    	scanf( "%c", &value.c, "%hd", value.s, "%d", &value.i, "%ld", &value.l );
    	display_union( value.c,value.s,value.i,value.l);
    
    	printf( "Please enter a short: ");
    	scanf( "%hd", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	display_union(value.c,value.s,value.i,value.l);
    
    	printf( "Please enter a int: ");
    	scanf( "%d", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	display_union(value.c,value.s,value.i,value.l);
    
    	printf( "Please enter a long: ");
    	scanf( "%ld", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
    	display_union(value.c,value.s,value.i,value.l);
    
    	return 0;
    
    }
    
    void display_union( char c, short s, int i, long l )
    {
    	printf( "char  c = %c\n", c );
    	printf( "short s = %hd\n", s );
    	printf( "int   i = %i\n", i );
    	printf( "long  l = %ld\n\n", l );
    }

  7. #7
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Quote Originally Posted by Sure
    Hows this... it works
    Code:
    void display_union(char c, short s, int i, long l);
    I think you're supposed to be passing the *union* to the function, not each of the contents of the union. so like Quzah said, instead of the way you have it above, do this:
    Code:
    void display_union(integer_union u);
    then figure out how to pass the union into that function, instead of passing the members of the union (quzah already said how to do it)
    Last edited by Cobras2; 06-29-2005 at 01:48 AM.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  8. #8
    Registered User Zed's Avatar
    Join Date
    Jun 2005
    Posts
    2
    scanf( "%c", &value.c, "%hd", value.s, "%d", &value.i, "%ld", &value.l );
    When you are using union, you are permitted to use on variable at a time only.
    that is , use either value.c or value.s or value.i or value.l

    If you want to use all of these, use strcuture.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Technically, they are only using one at a time, since everything is done sequentially. Or rather, they would be using them one at a time if that statement wasn't so horribly horribly wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions of the same size and pointers
    By holychicken in forum C Programming
    Replies: 9
    Last Post: 10-06-2008, 03:29 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 12:43 AM
  4. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 04:04 AM
  5. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM