struck by typedef struct...
DanKarron at UCBVAX.BERKELEY.EDU
DanKarron at UCBVAX.BERKELEY.EDU
Sun Dec 16 03:23:49 AEST 1990
>Subject: Re: struck by typedef struct...
>
>|> typedef struct MyTag {
>|> MyStruct a;
>|> void (*a_subroutine_transfer_address)(MyStruct *s);
>|> } MyStruct;
>|>
>|> This don't fly.
>
>I'm no authority on ANSI C (my coworker is), but I see three things wrong
>with this:
>
> 1) The compiler does not recognize MyStruct as a type since it
> is not yet defined.
>
> 2) The compiler does not know what size MyStruct is the first
> time you use it.
>
> 3) The definition of MyStruct is recursive which can't work.
>
>
>|> typedef struct MyTag {
>|> struct MyTag a; /* Forward reference is legal. */
>|> void (*a_subroutine_transfer_address)(struct MyTag *);
>|> } MyStruct;
>|>
>|> This does.
>
>This is slightly better because the compiler understands that "struct MyTag"
>is a type, but 2) and 3) are still there.
Yes, that bothers me too. But the Good Book Says:
Chapter 6.5 Page 139 (ANSI edition)
.. This recursive declaration of a node might look chancy, but it's correct. It
is illegal for a structure to contain an istance of its self but
struct tnode *left;
declares left to be a pointer to tnode , not a tnode itself.
Sayeth the Good Book in another section
Chapter 6.7, Page 146 :
It must be emphasized that a typedef declaration does not create a new
type in any sense: it merely adds a new name for some existing type....
In effect typedef is like a #define, except that since it is interperted
by the compiler, it can cope with textual substitutilns that are beyond
the capabilities of a preprocessor. ...
>
>Perhaps something got lost in the translation here, like you meant the
>first field to be a pointer, rather than the struct itself.
>
You are precisely correct. You get the gold pointed star for astuteness!
>-Gary
>
+-----------------------------------------------------------------------------+
| karron at nyu.edu (E-mail alias that will always find me) |
| Fax: 212 340 7190 * Dan Karron, Research Associate |
| . . . . . . . . . . . . . . * New York University Medical Center |
| 560 First Avenue \*\ Pager <1> (212) 397 9330 |
| New York, New York 10016 \**\ <2> 10896 <3> <your-number-here> |
| (212) 340 5210 \***\_________________________________________ |
| Main machine: karron.med.nyu.edu (128.122.135.3) IRIS 85GT |
+-----------------------------------------------------------------------------+
More information about the Comp.sys.sgi
mailing list