home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!jutta
- From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
- Subject: Re: Is this ANSI?
- Message-ID: <1992Dec27.211046.1846@cs.tu-berlin.de>
- Sender: news@cs.tu-berlin.de
- Organization: Techn. University of Berlin, Germany
- References: <BzqnK1.8t6@ais.org>
- Date: Sun, 27 Dec 1992 21:10:46 GMT
- Lines: 56
-
- Welcome to this week's installment of the tag-declaration-with-function-
- prototype-scope question. Our guest tonight, Patrick Draper (draper@ais.org)
- knows a compiler that ...
-
- > ... complained about the redeclaration of function foo. If I put the
- > structure definition before the prototype, Turbo C++ 1.01 is happy.
- [..]
- > void foo (struct bar *x);
-
- The tag (structure, union or enumeration type name) `bar' in this
- line has function prototype scope. After the closing ')', the compiler
- suffers from complete amnesia regarding `bar's.
-
- > struct bar {
- > int aaa;
- > int bbb;
- > };
-
- Thus, this one is a different struct `bar'.
-
- > void foo (struct bar *x)
-
- Because a struct bar is defined in the enclosing scope (between the
- declaration and the end of file, that is), this declaration only _uses_
- that bar -- it doesn't declare a new one.
-
- > My question is: what does ANSI say about this. Stylistically, I can see that
- > it is preferable to define the structure before I use it, however, it is
- > defined in the global realm, thus should be visible.
-
- Scopes do not extend backwards. They start after the declaration (for
- things like variable names) or just after the name has been mentioned
- (for unions, structs and enumerations), and end with the thing they're
- named after (file scope -- end of file, block scope -- closing '}', etc.)
- C is pretty much a one-pass-language in that regard; the compiler doesn't
- go back and fix things up for you.
-
- BTW: You don't need to define the structure before you use it, you just have
- to declare it.
-
- struct bar;
-
- void foo (struct bar *x);
-
- struct bar {
- ...
- };
-
- void foo (struct bar * x)
- {
- ...
- }
-
- will work fine.
-
- Jutta Degener (jutta@cs.tu-berlin.de)
-