casting to a union

alan l wendt wendt at handel.cs.colostate.edu
Wed Dec 13 11:46:39 AEST 1989


Apologies if this has come up before; I don't follow this group.

I have a function that accepts either char pointers or ints as
arguments, so I declare it with unions,

union numstr {
    int	i;
    char *s;
    };

void foo( union numstr ns ) { ... }


Why can't I call it as follows:

	foo((union numstr)7);

Presumably foo knows which it is getting by some external means.
This seems easy and innocuous, but I don't think the language
will let me do it.
Probably if this were allowed in, the backwards conversion would
also be allowed, i.e. from (union numstr) to (int).  That would
be ok with me, too; I actually prefer "(int)ns" to "ns.i".
Can anybody think of a reason not to allow this?

Alan Wendt



More information about the Comp.lang.c mailing list