home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!pacific.mps.ohio-state.edu!davis
- From: davis@pacific.mps.ohio-state.edu ("John E. Davis")
- Newsgroups: comp.lang.c
- Subject: compiler bug or (I hope) me?
- Message-ID: <DAVIS.92Sep2201102@pacific.mps.ohio-state.edu>
- Date: 3 Sep 92 01:11:02 GMT
- Sender: news@pacific.mps.ohio-state.edu
- Reply-To: davis@pacific.mps.ohio-state.edu (John E. Davis)
- Organization: "Dept. of Physics, The Ohio State University"
- Lines: 99
- Nntp-Posting-Host: pacific.mps.ohio-state.edu
-
- Hi,
-
- Consider the following program:
-
- #include <stdio.h>
- float fproto(float x)
- {
- fprintf(stdout, "In fproto, x = %f\n", x);
- return(2.0 * x);
- }
-
- float fkr(x)
- float x;
- {
- fprintf(stdout, "In fkr, x = %f\n", x);
- return(2.0 * x);
- }
-
- main()
- {
- float x,y;
- long f, *fp;
-
- x = 2.0;
- f = (long) fkr;
- fp = &f;
- y = ( (float (*)(float)) (*fp) )(x);
- fprintf(stdout, "from fkr: %f\n", y);
-
- x = 2.0;
- f = (long) fproto;
- fp = &f;
- y = ( (float (*)()) (*fp) )(x); /* <<---- see text below */
- fprintf(stdout, "from proto: %f\n", y);
- }
-
- Here I have assummed that long and long * have the same size. I have
- ran this using the mips c compiler and gcc on a dec station 5000, as
- well as Borland C++ 3.0 with the large memory module. In all cases, I
- obtained:
-
- In fkr, x = 2.000000
- from fkr: 4.000000
- In fproto, x = 0.000000
- from proto: 0.000000
-
- What am I doing wrong? I replaced the line referenced above with
-
- y = ( (float (*)(float)) (*fp) )(x); /* <<---- see text below */
-
- and it ran as expected when compiled under gcc and BCC++. However, it still
- refused to produce the desired results under the standard mips c compiler.
- Now when I replaced the first typecase for the pointer to the unprotyped
- function, as in:
-
- f = (long) fkr;
- fp = &f;
- y = ( (float (*float)(float)) (*fp) )(x);
-
- gcc failed for this function, BCC++ produced a floating point overflow, but
- the mips c compiler got it right!
-
- Please explain this behavior to me. I am confused because I thought that
- the typecast: '(float (*)())' casts a pointer to a function of type float
- with no information about its arguments. That is,
-
- float a0();
- float a1(float);
- float a2(float, float);
- etc....
-
- are all of the type (float (*)()). Stated another way, (float (*)(int)) is
- a subset of (float (*)()). Is this correct? Compare with the BCC++ manual:
- (page 55, programmers guide):
-
- ``...Under C++, which has stronger type checking, a pointer to a
- function has type ``pointer to function taking argument types
- `type' and returning `type'.'' In fact under C a function
- defined with argument types will also have this narrower type.
- For example:
-
- void (*funct)();
-
- In C, this is a pointer to a function returning nothing. In
- C++, it is a pointer to a function taking no arguments and
- returning nothing....''
-
- So, it seems to me that in C, the above program should work without the
- explicitly putting (float) in the argument list of the typecast.
-
- Any suggestions?
- --
- _____________
- #___/John E. Davis\_________________________________________________________
- #
- # internet: davis@amy.tch.harvard.edu
- # bitnet: davis@ohstpy
- # office: 617-735-6746
- #
-