Array of unions

Joseph S. D. Yao jsdy at hadron.UUCP
Wed Jul 3 11:51:22 AEST 1985


In article <27694 at lanl.ARPA> rmc at lanl.ARPA writes:
>> 	I defined an array of unions as follows:
>> 	    typedef union {
>> 		unsigned char *bytes;
>> 		unsigned long *words;
>> 	    } block;
>> 	    block sched[NKEYS];
>> 	Now if I try to pass sched[i] as a parameter,
>> the C compiler bombs with "Fatal error in /lib/ccom".
>	Here's an example of a program that didn't
>compile under 4.2/4.3bsd (VAX).  The program DID
>compile on a SUN, or with the portable C compiler
>under SUMacC.
>#define NKEYS 20
>	typedef union {
>		unsigned char *bytes;	unsigned long *words;
>	} block;
>	main()
>	{
>	block sched[NKEYS]; int i;
>	i= 4; bar(i, sched);
>	}
>
>	bar (i, sched) block sched[];
>	{ foo(sched[i]); }
>
>	foo (junk) block junk;
>	{ printf("%d\n",junk.bytes); }

As others have said, this should have worked.  Potential problems/
perturbations to test:
(1) in bar(), try changing sched[] to *sched.  As discussed ad
    infinauseum elsewhere, this  s h o u l d  make no difference.
(2) in bar(), pass &sched[i]; and in foo(), declare block *junk;.
    Also, of course, junk->bytes.  The construct you use there is
    relatively recent, and might cause problems that haven't been
    encountered before.  I know that passing structs as parameters
    is now old hat, but I think some compilers handle unions a
    little strangely.  Addresses (as in the old days) might be
    a bit better.
Good luck.  Write if it's found to work.
-- 

	Joe Yao		hadron!jsdy at seismo.{ARPA,UUCP}



More information about the Net.bugs mailing list