'struct' - declarations & parameters

Richard O'Keefe ok at edai.UUCP
Fri Feb 17 07:25:58 AEST 1984


     A couple of days ago I had occasion to port a large C program  from
a VAX to an Orion.  The C compiler on the Orion was not based on the PCC
but was derived directly from the book and the V7 addendum,  then hacked
until all the 4.1bsd programs were correctly compiled.

     A note and a question.

     The note is that they have two stacks (like the C machine  and  the
RISC chip) : one for scalars and one for arrays.  'struct' arguments are
passed on the array stack, and struct values returned there.  I can find
nothing in the rather sketchy descriptions of C available to forbid this.
Consequently,

	struct {int a, b, c;} foo = {1, 2, 3};

	main () { printf("%d %d %d\n", foo); }

which works just fine on a VAX, doesn't work on  an  Orion.   I  suspect
that it may not work on a RISC chip or on a C70 either.   Not  only  are
structs passed on the array stack.  So are unions.  I found out the hard
way  on  the  VAX that even when a union fits into 32 bits the PCC still
treats it as a "big" object, so the Orion C compiler  is  not  alone  in
treating  unions  differently  from integers etc.  The result of that is
that
	char xc = 12, *pc = &xc;
	double xd = 19.7, *pd = &xd;
	
	void bother(x, i)
	    union {char *c; double *d;} x;
	    int i;
	    {
		if (i) {
		    printf("%d ", *x.c);
		} else {
		    printf("%g\n", *x.d);
		}
	    }

	void main()
	    {
		bother(pc, 1);
		bother(pd, 0);
	    }

doesn't work either.  pc and pd are  pointers,  so  get  passed  on  the
scalar  stack,  while x is a union, so is looked for on the array stack.
Once again, this doesn't seem to be forbidden by available  descriptions
of C, and is likely to happen on other "C machines".

     The  question  is  whether  the  final  semicolon  in  a   'struct'
declaration  is  optional  or  not.   I  have been using two layouts for
structures:

	typedef struct foo
	    {
		type1 field1;	/* rem1 */
		...
		typen fieldn;	/* remn */
	    }	foo;

for important types; and for tiny little structs embedded in something:

	struct {type1 field1; ...; typen fieldn} dummy;

both of which the VAX C compiler is perfectly happy with.  But the Orion
compiler, being written from the book, insists on the amazingly ugly  ;}
in both cases.  Which compiler is right?



More information about the Comp.lang.c mailing list