Using a union as a function parameter

Discussion in 'C Programming' started by Juke All, May 27, 2004.

  1. Juke  All

    Juke All Guest

    When I compile the code (below), I get this error:
    cannot convert parameter 1 from 'int' to 'union dna'

    Without saying:

    FOO x;
    x.val = 100;

    ....is it possible to use a union as a function parameter, and when
    calling that function, pass the argument as one of the types of the
    union (int, in the following case)?


    typedef union foo
    {
    void* ptr;
    int val;
    } FOO;

    void* test(FOO obj)
    {
    void* ptr = obj.ptr;
    int val = obj.val;
    return NULL;
    }

    int main(int argc, char* argv[])
    {
    int x = 100;
    test(x);
    return 0;
    }
     
    Juke All, May 27, 2004
    #1
    1. Advertisements

  2. What the ^*&^& are you thinking here? You told the compiler that test()
    takes a FOO as a parameter and then you give it a stinking int? What part
    of the compiler error isn't clear? Try declaring 'x' as type FOO and try
    again.
     
    Mark A. Odell, May 27, 2004
    #2
    1. Advertisements

  3. Juke  All

    Juke All Guest


    I know that works, hence, the part of my message where I said:

    Without saying:
    FOO x;
    x.val = 100;

    I was just wondering if it was possible to pass an int into something
    that expects union foo (and one of the union members is an int).
     
    Juke All, May 27, 2004
    #3
  4. Of course not! That's the whole point of a typed language. If you want to
    save some typing you can initialize x at the point of definition like
    this:

    int main(void)
    {
    FOO x = { NULL, 100 };
    test(x);

    return 0;
    }

    This probably won't do what you want though. Also, realize you are passing
    a copy of x into test which is fine but if you expect test() to modify x
    for use by main() you will need to change test() to take a FOO * instead.
     
    Mark A. Odell, May 27, 2004
    #4
  5. Juke  All

    Malcolm Guest

    If union foo and int are the same size you might find a way of subverting
    the compiler's type system to do this, but what's the point?

    BTW this code

    void* test(FOO obj)
    (obj.val and obj.ptr share the same storage) is illegal - one may not write
    a member of a union as one type and read it as a second. This is often
    breached by platform-specific extensions, but is an ANSI rule.
     
    Malcolm, May 27, 2004
    #5
  6. eh-hem, I was thinking struct here. Ignore this post please. You can force
    the compiler to accept the int as a FOO via:

    int x = 100;

    test((FOO) x);
     
    Mark A. Odell, May 27, 2004
    #6
  7. Juke  All

    Default User Guest


    Even if you could, the function that was called wouldn't know which had
    been set. Your function here:


    would be dicey.


    [#5] With one exception, if the value of a member of a union
    object is used when the most recent store to the object was
    to a different member, the behavior is
    implementation-defined.


    What is it you are trying to do? Tell us your problem, not your broken
    solution.




    Brian Rodenborn
     
    Default User, May 27, 2004
    #7
  8. Juke  All

    Eric Sosman Guest

    Umpire: "Steee-riiike two!"

    Commentator: "Another bad swing at a pitch 'way outside
    the strike zone! That's not the Odell we're used to; something
    about this pitcher's dipsy-doodle side-arm delivery has got
    him baffled. Let's hope he gets un-baffled -- he digs in,
    looks out at the mound, here's the windup ..."
     
    Eric Sosman, May 27, 2004
    #8
  9. See, this is what happens when I try to think "bad". I simply cannot do it
    on purpose correctly. I do it fine accidentally, however. I'll try one
    more time to work around the C type system even though I wouldn't do this
    at home...

    int x = 100;

    test(*(FOO *) &x);
     
    Mark A. Odell, May 27, 2004
    #9
  10. In C89, you cannot do that without using a temporary union object. In
    C99, this works:

    int x = 100;
    test ((FOO){.val = x});
    If the `val' member of `obj' has been initialized, it results in
    undefined behavior to access the `ptr' member.
    Martin
     
    Martin Dickopp, May 27, 2004
    #10
  11. Juke  All

    j Guest

    heh, this is gold. This has to be the most humorous post I have ever
    read on c.l.c.
     
    j, May 28, 2004
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.