Thread: Problem with using a union into a struct :)

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Problem with using a union into a struct :)

    Hi to all!

    I'm trying to use a union into a struct.

    I do:

    Code:
    #include <stdio.h>
    
    typedef struct hello{
    int x;
    }hello;
    
    
    typedef struct hey {
    
    union p{
    
    int l = 8;
    hello *ptr;
    
    }types;
    
    int l;
    
    }hey;
    
    int main() {
    
    
    }
    and i get those warning and errors!

    union.c:12: warning: no semicolon at end of struct or union
    union.c:12: error: parse error before '=' token
    union.c:12: warning: no semicolon at end of struct or union
    union.c:15: error: parse error before '}' token
    union.c:15: warning: data definition has no type or storage class
    union.c:19: error: parse error before '}' token
    union.c:19: warning: data definition has no type or storage class


    What do i do wrong?

    Thanks in advance!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't assign a value to a union member inside of the union definition.
    Last edited by MacGyver; 05-12-2007 at 09:01 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Yes you are right! Thanks!
    Also, i have to add sthg else in order for the program to be compiled properly
    I shall add :

    Code:
    #include <stdio.h>
    
    typedef struct hello{
    int x;
    }hello;
    
    typedef struct hey {
    
    union p{
    
    int l = 8;
    struct hello *ptr;
    
    }types;
    
    int l;
    
    }hey;
    
    int main() {
    
    }
    Also if i do:

    struct hello{

    boolean x;

    };

    I get the error : error: parse error before "boolean"

    So that means that i can't define a boolean variable in a struct. Am i right or not?

    Thanks in advance

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is it so hard to use indentation?
    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.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well right now the program is small so indentation comes afterwards the program compiles (according to me of course )
    If it was very long I would use indentation

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    So, can we define a boolean into a struct???

    Thanks, in advance

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, indentation should come first as you write. It's meant to make the code easier to read, and hence easier to debug, maintain, etc.

    boolean isn't a datatype in C, so unless you define it yourself, or it's defined in some library, you can't use it.

    Here's an indented -- and slightly altered -- version of your program:

    Code:
    #include <stdio.h>
    
    typedef struct hello
    {
    	int x;
    }hello;
    
    typedef struct hey
    {
    	union p
    	{
    		int l;
    		struct hello *ptr;
    	}types;
    	int l;
    }hey;
    
    int main(void)
    {
    	return 0;
    }
    Much easier to read, isn't it?

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Modified again:

    Code:
    #include <stdio.h>
    
    typedef struct
    {
    	int x;
    }hello;
    
    typedef struct
    {
    	union
    	{
    		int l;
    		hello *ptr;
    	} types;
    	int l;
    }hey;
    
    int main(void)
    {
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by g_p View Post
    Well right now the program is small so indentation comes afterwards the program compiles (according to me of course )
    If it was very long I would use indentation
    .thgir ot tfel morf srettel eht tup d'I ,egassem gnol a saw siht fI.


  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Thank you guys!

    Yeah sure, the program is more easy for reading

    Thanks!

  11. #11
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Maybe you never thought about it but indentation should be done WHILE you're coding to make it easier for you to spot mistakes. Things like missing closing braces or wrongly put semicolons are easier to spot if you indent asap.

    You could also use an editor like eclipse with auto-code formatting and use that every now and then.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by KONI View Post
    Maybe you never thought about it but indentation should be done WHILE you're coding to make it easier for you to spot mistakes. Things like missing closing braces or wrongly put semicolons are easier to spot if you indent asap.
    Not only that, but it's much easier to do WHILE writing the code, since if one mechanically types each closing brace immediately after the corresponding open brace, one doesn't have to hunt down later which brace matches which.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Struct Problem
    By Mr.Modem in forum C Programming
    Replies: 5
    Last Post: 08-13-2005, 03:19 PM
  5. Union in a struct?
    By Hunter2 in forum C Programming
    Replies: 2
    Last Post: 06-13-2003, 11:45 AM