[comp.lang.c] ANSI typedef rules

rjshaw@ramius.llnl.gov (Robert Shaw) (03/15/91)

In regards to ANSI C, what (if anything) does the standard say about 
an identifer that is in the scope of both a typedef and a variable 
declaration by the same name?

For example, gcc 1.37.1 using -ansi -pedantic -W behaves as indicated on 
the following source (the numbers 0-2 are for reference later):

typedef int thing;

void f ()
{
  thing t1;          /** (0) **/

  t1 = 1;

  {
    int thing;       /** (1) compiler not confused yet **/

    thing = 0;
    h(thing);
  }

  {
    thing t2;
    int   thing;
    thing t3;        /** (2) breaks **/

    thing = 0;

    {
      thing t4;

      g(t1,t2,t3,t4,thing);
    }
  }
}

What does the standard have to say about all this ? Is the decl at (1)
legal ? should (2) be legal and gcc fails to conform ? 


What about C++ ? same/different rules about this issue ?


Thanx !

Rob Shaw, rjshaw@ocfmail.ocf.llnl.gov, (415) 423-3916 


===============================================================================
 Rob Shaw                                              rjshaw@ocfmail.llnl.gov
===============================================================================

torek@elf.ee.lbl.gov (Chris Torek) (03/15/91)

In article <793@llnl.LLNL.GOV> rjshaw@ramius.llnl.gov (Robert Shaw) writes:
>In regards to ANSI C, what (if anything) does the standard say about 
>an identifer that is in the scope of both a typedef and a variable 
>declaration by the same name?

A typedef makes a synonym for an existing type up until it is overriden
by a local (block scope) declaration for a variable; it is then hidden
(and therefore entirely unusable) until that block scope is ended.

Gcc 1.37.1 is doing exactly the right thing in your example.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

henry@zoo.toronto.edu (Henry Spencer) (03/17/91)

In article <793@llnl.LLNL.GOV> rjshaw@ramius.llnl.gov (Robert Shaw) writes:
>In regards to ANSI C, what (if anything) does the standard say about 
>an identifer that is in the scope of both a typedef and a variable 
>declaration by the same name?

There is no such thing.  Only one of the two will be visible at any
given place.  Taking your example:

>typedef int thing;
>  {
>    thing t2;
>    int   thing;
>    thing t3;        /** (2) breaks **/

The declaration of `thing' as an int variable hides the declaration as
a type name in the outer scope.  From the semicolon ending `int thing;'
until the closing brace, `thing' is the name of a variable, not a type.
-- 
"But this *is* the simplified version   | Henry Spencer @ U of Toronto Zoology
for the general public."     -S. Harris |  henry@zoo.toronto.edu  utzoo!henry

mike@sojurn.UUCP (Mike Sangrey) (03/25/91)

In article <1991Mar16.233150.4078@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <793@llnl.LLNL.GOV> rjshaw@ramius.llnl.gov (Robert Shaw) writes:
>>In regards to ANSI C, what (if anything) does the standard say about 
>>an identifer that is in the scope of both a typedef and a variable 
>>declaration by the same name?
>
>There is no such thing.  Only one of the two will be visible at any
>given place.  Taking your example:
>
>>typedef int thing;
>>  {
>>    thing t2;
>>    int   thing;
>>    thing t3;        /** (2) breaks **/
>
>The declaration of `thing' as an int variable hides the declaration as
>a type name in the outer scope.  From the semicolon ending `int thing;'
>until the closing brace, `thing' is the name of a variable, not a type.

I'm new around these net parts, but I think the answer is incorrect -- correct
me if I'm wrong -- I'm sure someone will ;-)  

The following is from X3J11/88-159 -- December '88 draft.  Sorry, don't
have the latest.

"A typedef name shares the same name space as other identifiers declared
in ordinary declarators.  If the identifier is redeclared in an inner
scope or is declared as a member of a structure or union in the same or
an inner scope, the type specifiers shall *not* be omitted in the inner
declaration." (emphasis mine)

Seems to me that 

typedef int stuff;
stuff stuff;

is fine.  Kinda confusing, but perfectly valid.

Mike @ The Sojournage Palace     "It muddles me rather." Winnie the Pooh.
rutgers!sci!devon!sojurn!mike

henry@zoo.toronto.edu (Henry Spencer) (03/27/91)

In article <232@sojurn.UUCP> rutgers!sci!devon!sojurn!mike (Mike Sangrey) writes:
>"A typedef name shares the same name space as other identifiers declared
>in ordinary declarators..."
>
>Seems to me that 
>
>typedef int stuff;
>stuff stuff;
>
>is fine...

Uh, no, sorry, it's not.  Take a look at that piece of ANSIspeak:  typedefs
are in the same name space as normal identifiers.  Your example is an attempt
to declare the same identifier with two different meanings, which is illegal.
-- 
"[Some people] positively *wish* to     | Henry Spencer @ U of Toronto Zoology
believe ill of the modern world."-R.Peto|  henry@zoo.toronto.edu  utzoo!henry