Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!kth!sunic!liuida!mikpe From: mikpe@majestix.ida.liu.se (Mikael Pettersson) Newsgroups: comp.lang.c Subject: Re: What is the scope of "type-name" in a cast expression? Summary: from the declaration point to the end of the block Message-ID: <1329@majestix.ida.liu.se> Date: 9 Aug 89 07:39:12 GMT References: <1324@majestix.ida.liu.se> Organization: CIS Dept, Univ of Linkoping, Sweden Lines: 45 In article <1324@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@mimsy (Chris Torek), mpl@attunix (Mike Lindner), karl@haddock (Karl Heuer) and dfp@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@ida.liu.se or ..!{mcvax,munnari,uunet}!enea!liuida!mpe