home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18914 < prev    next >
Encoding:
Text File  |  1992-12-27  |  2.1 KB  |  68 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!jutta
  3. From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
  4. Subject: Re: Is this ANSI?
  5. Message-ID: <1992Dec27.211046.1846@cs.tu-berlin.de>
  6. Sender: news@cs.tu-berlin.de
  7. Organization: Techn. University of Berlin, Germany
  8. References: <BzqnK1.8t6@ais.org>
  9. Date: Sun, 27 Dec 1992 21:10:46 GMT
  10. Lines: 56
  11.  
  12. Welcome to this week's installment of the tag-declaration-with-function-
  13. prototype-scope question.   Our guest tonight, Patrick Draper (draper@ais.org)
  14. knows a compiler that ...
  15.  
  16. > ... complained about the redeclaration of function foo. If I put the
  17. > structure definition before the prototype, Turbo C++ 1.01 is happy.
  18. [..]
  19. > void foo (struct bar *x);
  20.  
  21. The tag (structure, union or enumeration type name) `bar' in this
  22. line has function prototype scope.  After the closing ')', the compiler
  23. suffers from complete amnesia regarding `bar's.
  24.  
  25. > struct bar {
  26. >   int aaa;
  27. >   int bbb;
  28. > };
  29.  
  30. Thus, this one is a different struct `bar'.
  31.  
  32. > void foo (struct bar *x)
  33.  
  34. Because a struct bar is defined in the enclosing scope (between the
  35. declaration and the end of file, that is), this declaration only _uses_
  36. that bar -- it doesn't declare a new one.
  37.  
  38. > My question is: what does ANSI say about this. Stylistically, I can see that
  39. > it is preferable to define the structure before I use it, however, it is
  40. > defined in the global realm, thus should be visible.
  41.  
  42. Scopes do not extend backwards.  They start after the declaration (for
  43. things like variable names) or just after the name has been mentioned
  44. (for unions, structs and enumerations), and end with the thing they're
  45. named after (file scope -- end of file,  block scope -- closing '}', etc.)
  46. C is pretty much a one-pass-language in that regard; the compiler doesn't
  47. go back and fix things up for you.
  48.  
  49. BTW: You don't need to define the structure before you use it, you just have
  50. to declare it.
  51.  
  52.     struct bar;
  53.  
  54.     void foo (struct bar *x);
  55.  
  56.     struct bar {
  57.         ... 
  58.     };
  59.  
  60.     void foo (struct bar * x)
  61.     {
  62.         ...
  63.     }
  64.  
  65. will work fine.
  66.  
  67. Jutta Degener (jutta@cs.tu-berlin.de)
  68.