Why are typedef names in the same name space as variable names?

Gregory Smith greg at utcsri.UUCP
Mon Nov 17 04:25:56 AEST 1986


allou at brahms (Kenneth R. Ballou) writes:
>jgm at spice.cs.cmu.edu (John Myers) writes:
>>dlnash at ut-ngp.UUCP (Donald L. Nash) writes:
>>>Why are typedef names in the same name space as variable names?...
>>
>>If they weren't, then what would the output of the following program be?
>>
>>#include <stdio.h>
>>main()
>>{
>>    typedef char foo;
>>    long foo;
>>    
>>    printf("%d\n",sizeof(foo));
>>}
>
>I don't see that there is a question here.  Sizeof is *NOT* a function, as was
>quite thoroughly discussed some time back in this group.  Note that (foo) in
>this case is the syntax of a type cast, and in this case, sizeof returns 
	and also the syntax of a long expression.
>the size of a datum of type foo (in this case, 1).  If, however, you were to
>write  sizeof foo , you would then get the size of a long integer on your
>machine.  References:  K&R:  page 126 (explains sizeof (object) and also
>points out that sizes are given in unspecified units called "bytes," which
>are the same size as a char); page 187 (gives two forms of the *operator*
>sizeof, namely  sizeof expression  and  sizeof (type-name) ); page 188 (defines
>the meaning of the constructs involving sizeof and also notes that "byte"
>is the space required to hold a char *in all existing implementations*).
>
Objection sustained. If this were the only problem with putting typedefs
and variable names in separate spaces, then it would be probably be resolved
as you said in the given case. However, it ain't.

Having written a C parser, I can testify that it is imperative that the
two be in the same space. It must be possible to distinguish the end of
declarations at the start of a block. The last declaration ( if any )
is not delimited in any way - the parser must be able to determine that
the next 'thing' is a statement and not a declaration. Since
declarations are structured completely differently from statements, it
really helps if the distinction can be made very early ( i.e. after
looking at one token ).

typedef char foo1;
int foo2();
char *abc;
d(){
	foo1 (*xyz);	/* declare xyz as pointer to char */
	foo2 (*abc);	/* call foo2 with *abc as parameter */
	...
}
So if you allow typedefs and variables to be in separate spaces, and
change foo1 and foo2 to foo, how do tell whether foo(*abc) is a declaration
or a statement?
Less extreme examples can be constructed:
int foo3;  ... foo1 * xyz; This is also legal, but doesn't do anything.
Now, change foo1,foo3 to 'foo'. You can't expect the *parser* to
look at the construction "foo * xyz;" and think 'well that would be silly if
foo was an int, so it must be a typedef'.

"It is agreed that the ice is thin here."	- K&R, pg 206

-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list