Humberto Longoria
Humberto Longoria

Reputation: 1

How to pass the result of a function pointer as an argument for another function?

Alright, this is my first post ever in StackOverflow, I have TRIED everything for this. So first instruction is that I need to make a function pointer inside a struct. No problems so far with that.

typedef struct Function2D {
    float (*Fun2D) (float);
}Function2D;

The teacher wants this struct function pointer to multiply a given float number by two. I already did that.

int main() 
{
    Function2D Funcion;
    Funcion.Fun2D = Test;
    Funcion.Fun2D(10000);
    return 0;
}
float Test (float n)
{
    n = n*2;
    return n;
}

However I the teacher wants to implement another function that returns a Boolean after getting a data type Function2D or the result of the multiplication. Is there a way to access the result of the function pointer from a struct without hardcoding a value?

Upvotes: 0

Views: 63

Answers (1)

GoWiser
GoWiser

Reputation: 1045

If I understand your question correctly, then you are asking how to implement a function

  • returning a bool that takes a function as an argument

OR

  • that takes a structure containing function pointers as an argument.

OR

  • that pass the result from a call to a function pointer to another function.

Here is an example all three:

#include <stdio.h>

// Define the types for the function pointers you need
typedef float (*FloatFunc) (float);
typedef bool (*BoolFunc) (FloatFunc);

// Declare a structure containing each of the function pointer types defined above.
typedef struct StructContainingFunctionPointers {
    FloatFunc FunFloat;
    BoolFunc  FunBool;
} StructContainingFunctionPointers;

// Declare a function of the same type as FloatFunc
float MultiplyBy2(float n)
{
    n = n * 2;
    return n;
}

// Declare a function of the same type as BoolFunc
bool TestIfMultiplyBy2(FloatFunc func)
{
    return func(2) == 4;
}

// Declare a function that takes StructContainingFunctionPointers as an argument
void TestStructContainingFunctionPointers(StructContainingFunctionPointers Rec) {
    // Do a smoke-test of FunFloat
    float result = Rec.FunFloat(10000);
    // Pass the result of Rec.FunFloat to printf()
    printf("Calling FunFloat(10000) returned %.0f.\n", result);
    if (result == 10000 * 2)
        printf("The float function seems to work.\n");
    else
        printf("The float function DOES NOT work.\n");

    // Do a smoke-test of FunBool
    if (Rec.FunBool(Rec.FunFloat))
        printf("The bool function seems to work.\n");
    else
        printf("The bool function DOES NOT work.\n");
}

int main()
{
    // Initialize the record.
    StructContainingFunctionPointers Rec;
    Rec.FunFloat = MultiplyBy2;
    Rec.FunBool = TestIfMultiplyBy2;
    // Test
    TestStructContainingFunctionPointers(Rec);
    // Return success
    return 0;
}

This is the output from the program:

Calling FunFloat(10000) returned 20000.
The float function seems to work.
The bool function seems to work.

Upvotes: 1

Related Questions