Thread: Unions & Structs... Puzzled.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Unions & Structs... Puzzled.

    Can someone explain maybe with simple examples about how structs and more importantly nesting a union in a struct works?

    right now i have something like~

    Code:
    typedef struct Operator
    {
    	char symbol;
    	int operands;
    	union
    	{
    		void *f;
    		double (*f0)(void);
    		double (*f1) (double x);
    		double (*f2) (double x, double y);
    	}
    }Operator;
    the basic premise is it's a "calculator" of sorts... I'm using the struct to define various evaluations methods i have like addition/subtraction etc... The union comes in because the calculator needs to be versatile and should be able to work with any new functions i put in later. Being able to work with zeroary, unary, and binary operators simultaenously without needing to add new code...

    I'm confused about the union completely I'm not really sure how it works. Structs I know a little about.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A union basically lays all it's members "on top" of each other - so f, f1, f2, and f3 would occupy the same bit of space. For all other intents and purposes, a union works exactly like a struct. [Obviously, as a consequence of the "all members at the same place", if you change one member variant, all others will automatically change too - and it's quite possible, if you for example mix integer and float types, to get values that make no sense when viewed as a different type].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by matsp View Post
    A union basically lays all it's members "on top" of each other - so f, f1, f2, and f3 would occupy the same bit of space. For all other intents and purposes, a union works exactly like a struct. [Obviously, as a consequence of the "all members at the same place", if you change one member variant, all others will automatically change too - and it's quite possible, if you for example mix integer and float types, to get values that make no sense when viewed as a different type].

    --
    Mats
    I understand that much thank you... I'm more wondering how it works when I declare a struct... How do I work my evaluate functions in there?

    I have a function

    Code:
    double max1 (double x, double y)
    {
    	return x>y?y:x;
    }
    for example that determines the maximum of two numbers and it's a binary operation...

    Usually you would put:
    Code:
    Operator blah={'M',2,max1};
    M is my symbol.. a char, 2 the # of operators, and max1 the function... Where/how does the union fit in?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your Operator blah looks correctly initialized, and max1 is the value of the union.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by matsp View Post
    Your Operator blah looks correctly initialized, and max1 is the value of the union.

    --
    Mats

    Hmm I think I see, and since it takes two operators I can call it with *f2 ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sparrowhawk View Post
    Hmm I think I see, and since it takes two operators I can call it with *f2 ?
    Yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by matsp View Post
    Yes.

    --
    Mats
    Hmm, how would this be useful in implementing calculator-like functions?

    If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

    How would it work ?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It will actually always use the first member of the union in that case. To access a different member, you would have to use '.' notation.

    However in this case since sizeof(void*) and sizeof( double (*) (double ) ) are usually the same, the max passed will be usable with .f2, assuming that your compiler lets you compile that.
    Last edited by King Mir; 12-14-2008 at 04:39 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sparrowhawk View Post
    Hmm, how would this be useful in implementing calculator-like functions?

    If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

    How would it work ?
    What? Each member object gets its own data. So if you had Operator blah, bar, foo, baz; then each one would get its own symbol, its own operands, and its own union.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Sparrowhawk View Post
    Hmm, how would this be useful in implementing calculator-like functions?

    If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

    How would it work ?
    The code you have will alow you to have a single container, such as an array, of all of your zero-ary, unairy, and binary functions. To actually use the functions you would have to also know the arguements to pass to it. Then you can check if the number of arguments match the Operator, and if they do call the function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    What? Each member object gets its own data. So if you had Operator blah, bar, foo, baz; then each one would get its own symbol, its own operands, and its own union.
    Oh, of course... Heh heh guess I confused myself there for a second...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  2. unions and structs
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-24-2008, 10:54 PM
  3. why unions and structs couldnt be initialized ?
    By black in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2002, 05:05 AM
  4. Structs and Unions
    By Encrypted in forum C Programming
    Replies: 4
    Last Post: 11-05-2002, 03:53 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM