al712@unh.UUCP (Anthony Lapadula) (05/04/89)
Are you allowed to 'forward reference' enumerated types? In other words, is this legal? enum a b; /* 'enum a' is not yet defined */ Does 'b' end up as an int, or is this an error? Also, how about this? char *ptr; enum a { b }; /* 'b' has value 0 in expressions, right? */ if (ptr == b) /* Same as (ptr == NULL) ? */ foo(); (I don't have a copy of the ANSI C Draft for reference, and I was born and bred under K&R I.) Thanks, Anthony Lapadula ..uunet!unh!al712
chris@mimsy.UUCP (Chris Torek) (05/04/89)
In article <1152@unh.UUCP> al712@unh.UUCP (Anthony Lapadula) writes: >Are you allowed to 'forward reference' enumerated types? No. >Also, how about this? > > char *ptr; > enum a { b }; /* 'b' has value 0 in expressions, right? */ > > if (ptr == b) /* Same as (ptr == NULL) ? */ > foo(); Modulo some waffling about what constitutes an `integer constant zero', all C compilers should accept this. (I would not object to a warning, myself.) The way one obtains a nil pointer of type T is to write an expression which demands or produces a pointer of type T but is in fact an integer constant zero. To me, `the integer constant zero' means `any constant expression whose type is one of the integral types and whose value is zero'. According to the pANS, the type of an enumeration member is integral (I forget whether it is |int| or merely one of |signed char|, |short|, |int|, or |long|, but it is not unsigned ... at least, I think I would remember if it were allowed to be unsigned). So we have <pointer> <comparison-op> <integer-zero>. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
ark@alice.UUCP (Andrew Koenig) (05/04/89)
In article <1152@unh.UUCP>, al712@unh.UUCP (Anthony Lapadula) writes: > Are you allowed to 'forward reference' enumerated types? In other > words, is this legal? > enum a b; /* 'enum a' is not yet defined */ > Does 'b' end up as an int, or is this an error? It's an error, both in C++ and ANSI C. > Also, how about this? > char *ptr; > enum a { b }; /* 'b' has value 0 in expressions, right? */ Right. > if (ptr == b) /* Same as (ptr == NULL) ? */ > foo(); Yes, if NULL==0, by virtue of the fact that b is 0. However, the following is illegal: enum a1 {b1, c1}; if (ptr == c1) foo(); because the only integral value that may be meaningfully compared to a pointer is 0. -- --Andrew Koenig ark@europa.att.com