简体   繁体   中英

pointer to function c language basic

This is my first time working with pointers to function.

What I'm trying to do is create a function called essay, that gets a pointer to another function , an integer num, and then num doubles.

The function essay, will multiply the arguments, and then return the value of the function i recieved as an argument, with the product.

This sounds complex but it really is quite simple. Example:

essay(sin,2,pi,1/2) will return the value of sin(pi/2)

this is my code...for some reason it doesnt let me send the pointer to the function sin. Says no instance of overloaded function sin matches argument list, but this is exactly how i saw my teacher do it...I think.

#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
#include <math.h>
double (*pfunc)(double);
double essay(double* pfunc(double),double num, ... )
{
    int i;
    double product=1,result;
    va_list arguments;
    va_start(arguments,num);
    for(i=0;i<num;i++)
        product*=va_arg(arguments,double);
    va_end(arguments);
    result=*(pfunc(product));
    return result;
}
void main()
{
    double x,y;
    x=3.14159265358979323846;
    y=0.5;
    printf("%lf",essay(sin,2,x,y));
    getch();
}

this is wrong

double essay(double* pfunc(double),double num, ... )

Here you're passing a function as a parameter which return a pointer to double, that not make sense, should be:

 double essay(double (*pfunc)(double),double num, ... )

Here you're passing a pointer to a function which returns a double and receive a double as a parameter

#include <stdio.h>
#include <stdarg.h>
#include <math.h>
// removed extra var
double essay(double (*pfunc)(double), double num, ...) // added parens
{
    int i;
    double product = 1, result;
    va_list arguments;
    va_start(arguments, num);
    for (i = 0; i < num; i++)
    product *= va_arg(arguments, double);
    va_end(arguments);
    result = pfunc(product); // removed extra parens
    return result;
}

void main()
{
    double x, y;
    x = 3.14159265358979323846;
    y = 0.5;
    printf("%lf", essay(sin, 2, x, y));
}

This is what I'd do, assuming a C99 compiler that accepts variable declarations in for loops and at arbitrary points in a block of code. Note the use of the typedef for the function pointer type ( MathFunc2 would be for a function that takes two arguments, etc), and the use of int (rather than double ) for the number of values in the argument list.

#include <math.h>
#include <stdarg.h>
#include <stdio.h>

typedef double (*MathFunc1)(double);

static double essay(MathFunc1 function, int num, ...)
{
    double product = 1.0;
    va_list arguments;
    va_start(arguments, num);
    for (int i = 0; i < num; i++)
        product *= va_arg(arguments, double);
    va_end(arguments);
    double result = (*function)(product);
    return result;
}

int main(void)
{
    double x = 3.14159265358979323846;  // M_PI?
    double y = 0.5;
    printf("%f\n", essay(sin, 2, x, y));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM