[comp.lang.c] strange effects with enum types

phrrngtn@cs.tcd.ie (Paul Harrington) (10/14/88)

The following code was tested on a SUN 3/50 under SunOS 3.3.
Depending on wheter you give an initialiser to the enum elements in the
declaration of the type of p or not you get very different outputs from
test_fn() ranging from 1,3,5 to 1,2,4 to 2,4,6 with my old favourite 2,3,4 when
you say a=1,b=2,c=3 in the declaration of the type of p in test_fn()

An RPC package called Courier generates this unusual type of code with
parameter types being redeclared  even for functions that have the same
effective types for their parameters.

why does the compiler (I don't know which compiler it is. I assume it is
the standard one supplied with a SUN 3/50) produce this result and is it a bug of this compiler or a "feature" of C ?





typedef struct {
	enum{a=1,b,c}choice;
	union{
	int a;
	long b;
	short c;
	}u;
	}new_struct;

typedef enum {e,f,g,h} e1;


void test_fn1(p)
struct {
	enum{a=1,b=2,c=3}choice;
	union{
	int a;
	long b;
	short c;
	}u;
	}*p;
{
printf("the size of p is %d\n",sizeof(p));
printf("(test_fn1) a=%d,b=%d,c=%d\n",a,b,c);
}

main()
{
new_struct *v;
printf("(main) a=%d,b=%d,c=%d\n",a,b,c);
printf("the size of e1 is %d\n",sizeof(e1));
printf("the size of a is %d\n",sizeof(a));
test_fn1(v);
}

chris@mimsy.UUCP (Chris Torek) (10/20/88)

In article <143@csaran.cs.tcd.ie> phrrngtn@cs.tcd.ie (Paul Harrington) writes:
[everything unnecessary for bug invocation deleted]
>enum{a=1,b,c}choice;
>void f(p) struct { enum{a=1,b=2,c=3}choice; }*p; {
>	printf("(f) a=%d,b=%d,c=%d\n",a,b,c); }
>main() { printf("(main) a=%d,b=%d,c=%d\n",a,b,c); f(); }

[prints
	(main) a=1,b=2,c=3
	(f) a=2,b=3,c=4
]

>why does the compiler ... produce this result and is it a bug of this
>compiler or a "feature" of C ?

The compiler is trying to be clever, and goofing.  See my fix in
comp.bugs.4bsd.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris