What is the scope of "type-name" in a cast expression?

Mikael Pettersson mikpe at majestix.ida.liu.se
Wed Aug 9 17:39:12 AEST 1989


In article <1324 at majestix.ida.liu.se> I asked:
>  What is the scope of the "type-name" in a cast expression?
> [example of legal, but ugly, code omitted]
>
>AdvTHANX for any references to the appropriate "chapter and verse".

(Thanks to cris at mimsy (Chris Torek), mpl at attunix (Mike Lindner),
karl at haddock (Karl Heuer) and dfp at cbnewsl (Dave Prosser) for
pointing me in the right direction.)

K&R1, the appendix, section 11.1:
	"Members and tags follow the same scope rules as other
	 identifiers. The enum constants ... follow the same scope rules."

ditto, section 8.5:
	"A structure [or union] specifier [of the form]
		struct identifier { struct-decl-list }
	 declares the identifier to be the structure tag..."

ditto, section 8.6:
	[enumeration tags behave just like struct/union tags]

ditto, section 16.2:
	"type-name:		type-specifier ...
	 type-specifier:	struct-or-union-specifier | ..."

This means that the "type-name" in a cast (or sizeof) expression
may introduce new names in the closest surrounding block. They are
visible from the declaration point to the end of the block.

Now why didn't I see this before :-( [at 1 AM !? gimme a break]
(I'm in good company though; H&S-2nd ed. page 121 makes the same mistake:
	/* struct S is not defined here */
	i = sizeof( struct S { ... });	/* OK, but strange */
	j = sizeof( struct S );		/* probably illegal because
					struct S is still not defined */
:-> )

/Mike
(p.s. In case you're wondering why on earth one would want to examine
dubious constructs like these, it's because I'm working on a denotational
definition for C. It'll be a rather messy one..)
-- 
Mikael Pettersson, Dept of Comp & Info Sci, University of Linkoping, Sweden
email: mpe at ida.liu.se  or  ..!{mcvax,munnari,uunet}!enea!liuida!mpe



More information about the Comp.lang.c mailing list