Write a function that takes as parameter a pointer to any of the functions

Olivier Piasecki

I'm struggling with pointers in C. I have to write a program that will make a 3 operations on one number (sqrt, sin, exp) using functions and pointers. I have done it but I have a problem to write a function that takes as parameter a pointer to any of the above functions + a floating point number, returning the result of the corresponding calculation. In main function I attached working code without this function. I don't have any idea how to write this function and I wrote my try in this code. Here I attach code which I have wrote:

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

double square(double *);
double sinus(double *);
double exponential(double *);
void count(*square, double result);

double number, square_result, sinus_result, exponential_result;

int main()
{

    printf("Give number: ");
    scanf("%lf", &number);

    square_result = square(&number);
    sinus_result = sinus(&number);
    exponential_result = exponential(&number);

    printf("%lf\n", square_result);
    printf("%lf\n", sinus_result);
    printf("%lf", exponential_result); 

}

double square(double *x)
{
    double square_result;
    square_result = sqrt(*x);

    return square_result;
}

double sinus(double *x)
{
    double sinus_result;
    sinus_result = sin(*x);

    return sinus_result;
}

double exponential(double *x)
{
    double exponential_result;
    exponential_result = exp(*x);

    return exponential_result;
}

void count(*square, double result)
{
    result = square(&number);
}
Pat. ANDRIA

I created a function call_functaking a pointer on a function as an argument and a double* like the following:

//arguments are a pointer to any functions taking argument double* and a double* number
double call_func(double (*MyFunction)(double* arg), double* number)
{
    return (MyFunction)(number);
}

// a small edit, imho you do not need to pass number by pointer

This shall be called like this:

    square_result = call_func(square, number);
    sinus_result = call_func(sinus, number);
    exponential_result = call_func(exponential,number);

Here it is, integrated in your code:


// here is my function that takes a function pointer as an argument
double call_func(double (*MyFunction)(double* arg), double number);


int main()
{

    printf("Give number: ");
    scanf("%lf", &number);

    square_result = square(&number);
    sinus_result = sinus(&number);
    exponential_result = exponential(&number);

    printf("%lf\n", square_result);
    printf("%lf\n", sinus_result);
    printf("%lf", exponential_result);
    // call of my general function

    printf("\n********************************\n");
    printf("\n\nUSAGE OF FUNCTION POINTER \n");

    double square_result2 = 0.0;
    double sinus_result2 = 0.0;
    double exponential_result2 =0.0;

    square_result2 = call_func(square, &number);
    sinus_result2 = call_func(sinus, &number);
    exponential_result2 = call_func(exponential, &number);

    printf("%lf\n", square_result2);
    printf("%lf\n", sinus_result2);
    printf("%lf", exponential_result2);

}


//arguments are a pointer to any functions taking argument double and a double number
double call_func(double (*MyFunction)(double* arg), double* number)
{
    return (MyFunction)(number);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

A function that takes string parameter and returns integer pointer

From Dev

Allow template parameter of function pointer type to accept functions of any return type

From Dev

How to forward declare a function that takes a pointer to a function as a parameter?

From Dev

Function Pointer parameter from any class

From Dev

Write padding bytes ('0') with a function that takes a pointer and a length as arguments

From Dev

Accept any function (but only functions) as a parameter

From Dev

Parse network 'interfaces' for read/write using pointer as parameter of functions

From Dev

OCaml Function that takes in functions

From Dev

Constructor that takes any delegate as a parameter

From Dev

Connect function that takes Write to function that takes Read

From Dev

Function Pointer as Function Parameter

From Dev

Pointer to pointer parameter in Swift function

From Dev

Pointer to Pointer to Void Function Parameter

From Dev

PInvoke function with pointer to pointer parameter

From Dev

void pointer in function parameter

From Dev

Integer pointer as parameter of function

From Dev

void pointer in function parameter

From Dev

Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings

From Dev

memoizing a function that takes a set as parameter

From Dev

Create a function that takes a UIViewController as a parameter

From Dev

C++: overloading a function with a void* function with functions with specific(double...) pointer parameter

From Dev

Function with void *a and pointer function as parameter

From Dev

How to declare a function in a header that takes a pointer as an argument?

From Dev

Pointer to function parameter vs function parameter?

From Dev

Pointer to function parameter vs function parameter?

From Dev

MissingMethodException when testing a function that takes a function parameter

From Dev

A function that returns an array, but that also takes a function as parameter

From Dev

Write function with type parameter

From Dev

Function not accepting any parameter

Related Related

  1. 1

    A function that takes string parameter and returns integer pointer

  2. 2

    Allow template parameter of function pointer type to accept functions of any return type

  3. 3

    How to forward declare a function that takes a pointer to a function as a parameter?

  4. 4

    Function Pointer parameter from any class

  5. 5

    Write padding bytes ('0') with a function that takes a pointer and a length as arguments

  6. 6

    Accept any function (but only functions) as a parameter

  7. 7

    Parse network 'interfaces' for read/write using pointer as parameter of functions

  8. 8

    OCaml Function that takes in functions

  9. 9

    Constructor that takes any delegate as a parameter

  10. 10

    Connect function that takes Write to function that takes Read

  11. 11

    Function Pointer as Function Parameter

  12. 12

    Pointer to pointer parameter in Swift function

  13. 13

    Pointer to Pointer to Void Function Parameter

  14. 14

    PInvoke function with pointer to pointer parameter

  15. 15

    void pointer in function parameter

  16. 16

    Integer pointer as parameter of function

  17. 17

    void pointer in function parameter

  18. 18

    Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings

  19. 19

    memoizing a function that takes a set as parameter

  20. 20

    Create a function that takes a UIViewController as a parameter

  21. 21

    C++: overloading a function with a void* function with functions with specific(double...) pointer parameter

  22. 22

    Function with void *a and pointer function as parameter

  23. 23

    How to declare a function in a header that takes a pointer as an argument?

  24. 24

    Pointer to function parameter vs function parameter?

  25. 25

    Pointer to function parameter vs function parameter?

  26. 26

    MissingMethodException when testing a function that takes a function parameter

  27. 27

    A function that returns an array, but that also takes a function as parameter

  28. 28

    Write function with type parameter

  29. 29

    Function not accepting any parameter

HotTag

Archive