[comp.sys.sun] Union Problem on Sparc

brad@ds3.bradley.edu (Bradley E. Smith) (11/04/90)

Hi, I realize that the program below is not quite right(or proper) but I
am trying to move our 'pic' source from a VAX to a SUN SPARC.  The below
program works ok on a VAX, AT&T 3B2, IBM RT but dumps core on a SUN.  I
always thought that unions were passed by value.  Anyways I need this
program to work, or I will have to rewrite a bunch of things.

Is it me or is the compiler bugging (FYI: 4.0.3 and 4.1 have the same
problem, and gcc does the same thing).

union u {
	int i;
	long l;
	char c;
	float f;
	char *cp;
};


main()
{
	union u x,z;

	x.i = 100;
	z.i = 99;
	printf("sizeof(u) = %d\n", sizeof(x));
	printf("sizeof(int) = %d\n", sizeof(int));
	(void) doit(x,z);
	(void) doit(0,0);
	exit(0);
}
doit(a,b)
union u a;
union u b;
{
	(void) printf("%d, %d\n", a.i, b.i);
}

henry@zoo.toronto.edu (Henry Spencer) (11/21/90)

In article <1990Nov4.031554.3208@rice.edu> brad@ds3.bradley.edu (Bradley E. Smith) writes:
>	union u x,z;
>	(void) doit(x,z);
>	(void) doit(0,0);

Did you try running "lint" on this?  You will find type mismatches.  `0'
is not a union value; it cannot be passed to a function that is expecting
to get a union.  On some machines this accidentally works, but on a lot of
modern ones, it doesn't.  There is no such thing as a "union constant" in
C, so you'll have to assign `0' to a union variable and pass the variable.

"I don't *want* to be normal!"         | Henry Spencer at U of Toronto Zoology
"Not to worry."                        |  henry@zoo.toronto.edu   utzoo!henry