home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13134 < prev    next >
Encoding:
Text File  |  1992-09-02  |  3.2 KB  |  112 lines

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!pacific.mps.ohio-state.edu!davis
  2. From: davis@pacific.mps.ohio-state.edu ("John E. Davis")
  3. Newsgroups: comp.lang.c
  4. Subject: compiler bug or (I hope) me?
  5. Message-ID: <DAVIS.92Sep2201102@pacific.mps.ohio-state.edu>
  6. Date: 3 Sep 92 01:11:02 GMT
  7. Sender: news@pacific.mps.ohio-state.edu
  8. Reply-To: davis@pacific.mps.ohio-state.edu  (John E. Davis)
  9. Organization: "Dept. of Physics, The Ohio State University"
  10. Lines: 99
  11. Nntp-Posting-Host: pacific.mps.ohio-state.edu
  12.  
  13. Hi,
  14.  
  15.     Consider the following program:
  16.  
  17. #include <stdio.h>
  18. float fproto(float x)
  19. {
  20.    fprintf(stdout, "In fproto, x = %f\n", x);
  21.    return(2.0 * x);
  22. }
  23.  
  24. float fkr(x)
  25. float x;
  26. {
  27.    fprintf(stdout, "In fkr, x = %f\n", x);
  28.    return(2.0 * x);
  29. }
  30.  
  31. main()
  32. {
  33.    float x,y;
  34.    long f, *fp;
  35.    
  36.    x = 2.0;
  37.    f = (long) fkr;
  38.    fp = &f;
  39.    y = ( (float (*)(float)) (*fp) )(x);
  40.    fprintf(stdout, "from fkr: %f\n", y);
  41.    
  42.    x = 2.0;
  43.    f = (long) fproto;
  44.    fp = &f;
  45.    y = ( (float (*)()) (*fp) )(x);   /* <<---- see text below */
  46.    fprintf(stdout, "from proto: %f\n", y);
  47. }
  48.  
  49. Here I have assummed that long and long * have the same size.  I have
  50. ran this using the mips c compiler and gcc on a dec station 5000, as
  51. well as Borland C++ 3.0 with the large memory module.  In all cases, I
  52. obtained:
  53.  
  54. In fkr, x = 2.000000
  55. from fkr: 4.000000
  56. In fproto, x = 0.000000
  57. from proto: 0.000000
  58.  
  59. What am I doing wrong? I replaced the line referenced above with 
  60.  
  61.    y = ( (float (*)(float)) (*fp) )(x);   /* <<---- see text below */
  62.  
  63. and it ran as expected when compiled under gcc and BCC++.  However, it still
  64. refused to produce the desired results under the standard mips c compiler.
  65. Now when I replaced the first typecase for the pointer to the unprotyped
  66. function, as in:
  67.  
  68.    f = (long) fkr;
  69.    fp = &f;
  70.    y = ( (float (*float)(float)) (*fp) )(x);
  71.    
  72. gcc failed for this function, BCC++ produced a floating point overflow, but
  73. the mips c compiler got it right!
  74.  
  75. Please explain this behavior to me.  I am confused because I thought that
  76. the typecast: '(float (*)())' casts a pointer to a function of type float
  77. with no information about its arguments. That is,
  78.  
  79. float a0();
  80. float a1(float);
  81. float a2(float, float);
  82. etc....
  83.  
  84. are all of the type (float (*)()).   Stated another way, (float (*)(int)) is
  85. a subset of (float (*)()).  Is this correct?  Compare with the BCC++ manual:
  86. (page 55, programmers guide):
  87.  
  88.       ``...Under C++, which has stronger type checking, a pointer to a
  89.       function has type ``pointer to function taking argument types
  90.       `type' and returning `type'.'' In fact under C a function
  91.       defined with argument types will also have this narrower type.
  92.       For example:
  93.  
  94.          void (*funct)();
  95.  
  96.       In C, this is a pointer to a function returning nothing.  In
  97.       C++, it is a pointer to a function taking no arguments and
  98.       returning nothing....''
  99.  
  100. So, it seems to me that in C, the above program should work without the
  101. explicitly putting (float) in the argument list of the typecast.
  102.  
  103. Any suggestions?  
  104. --
  105.      _____________
  106. #___/John E. Davis\_________________________________________________________
  107. #
  108. # internet: davis@amy.tch.harvard.edu
  109. #   bitnet: davis@ohstpy
  110. #   office: 617-735-6746
  111. #
  112.