prototyping again--- what about this one?

John E. Davis davis at pacific.mps.ohio-state.edu
Sun Nov 11 21:44:27 AEST 1990


Hi,

   I want to prototype a function returning double that takes 2 parameters:

          1.  pointer to double function(double)
	  2.  double

Here is how I did it for the mips c compiler on ds3100 system:

#include <stdio.h>

extern double g (double);
extern double funct(double (*)(double),double);

void main()
{
    double y,x;

    x = 3.0;
    y = funct(g,x);
    printf("%lf\n",y);
}

double funct(double (*h)(double),double x)
{

      return(h(x));
}

double g(double x)
{
      return (x*x);
}
/* end of example */

I compile and run it and I get the correct answer (9).  However if I compile
it on a sun 4 using the 'CC' c++ preprocessor which accepts ansi prototypes,
the program generates errors all around the place.  So I change it to:

#include <stdio.h>
typedef double (*PF)(double);  /* pointer to function(double) */

extern double g(double);
extern double funct(PF,double);

void main()
{
    double y,x;

    x = 3.0;
    y = funct(g,x);
    printf("%lf\n",y);
}

double funct(PF h,double x)
{

      return(h(x));
}

double g(double x)
{
      return (x*x);
}
/* end of example */

This satisfies the CC ``compiler''.  However the mips cc on the ds3100 doesn't
pick up the FP typedef and so the programs fails to compile. For example, lint
gives messages like '(5) PF redefinition hides earlier one'.

Now there are perhaps several problems:

   1. The  mips cc and CC preprocessor are not ansi.
   2. One of the compilers has a bug. (not likely)
   3. I don't know what the hell I am doing. Flame me. 
   4. All of the above.

What is the portable solution?

Thanks,
--
John

  bitnet: davis at ohstpy
internet: davis at pacific.mps.ohio-state.edu



More information about the Comp.lang.c mailing list