home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16809 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  2.6 KB

  1. Path: sparky!uunet!stanford.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: question about duplicate declarations
  5. Date: 19 Nov 1992 19:00:19 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 53
  8. Message-ID: <27543@dog.ee.lbl.gov>
  9. References: <1992Oct17.104808.29033@mccc.edu> <1849@lysator.liu.se> <26961@dog.ee.lbl.gov> <1992Oct26.172233.2045@mccc.edu> <26987@dog.ee.lbl.gov> <720329596@sheol.UUCP> <2R28E4L@math.fu-berlin.de>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <2R28E4L@math.fu-berlin.de> rene@hamel.uucp (Rene Mueller) writes:
  14. >In a function there couldn't be a[nother] fuction definition..
  15.  
  16. True enough.  Function *declarations* are, however, permitted.
  17.  
  18. >So code like
  19. >typ f1(paramlist)
  20. >{
  21. >typ f2(paramlist);
  22. >    .
  23. >    .
  24. >    .
  25. >}
  26. >Shouldn't be in ANY programm...
  27.  
  28. ANSI C allows code of the above form.  The declaration for f2()
  29. gives it the corresponding type, and the identifier goes out of scope
  30. at the closing brace.
  31.  
  32. (Stylistically, I happen to agree; function declarations with block
  33. scope provide nothing that the same declaration with file or global
  34. scope would give, except the chance to make a mistake.)
  35.  
  36. >and things like
  37. >typ f();
  38. >means that f is a function with NO parameter returning a value from type typ
  39.  
  40. No.  In ANSI C, an empty parameter list means `the number and types of
  41. the parameters are unspecified'.  This imposes a separate set of type
  42. compatibility rules.  Note that this differs from C++, where `int f()'
  43. means the same as ANSI C's `int f(void)'.
  44.  
  45. >... so typ f(paramlist) is a redeclaration with WRONG type
  46.  
  47. In ANSI C, the redeclaration is compatible (I called it `benign') if
  48. and only if the return type is compatible, the parameters in the
  49. `paramlist' all have `wide' types, and the actual function does not
  50. take variable arguments.
  51.  
  52. An incompatible (`malignant') redeclaration is apparently permitted
  53. under ANSI C.  If you call a function whose current in-scope
  54. declaration is incompatible with the actual type of the actual
  55. function, the result is undefined.  A future version of the standard
  56. will probably outlaw malignant redeclarations (note, this is sheer
  57. speculation on my part, and it does not completely eliminate the
  58. problem, only one aspect of it).
  59.  
  60. Note that any compiler can issue warnings at any time, so a good compiler
  61. can already warn about malignant redeclarations.  A diagnostic is not
  62. required, but no diagnostic is ever forbidden.
  63. -- 
  64. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  65. Berkeley, CA        Domain:    torek@ee.lbl.gov
  66.