0

So I was trying to accomplish to write a struct with pointer functions in it and this is what I wrote

typedef struct Coordinates {
    float x; 
    float y;
} Coord;

typedef struct Parallelograms {
        Coord upperR, lowerL;
        float base;
        float (*areaFunc)(float base, Coord upperR, Coord lowerL);
        float (*perimeter)(float base, Coord upperR, Coord lowerL);
} Parallelogram;

This is where I define the function:

float area(float base, Coord upperR, Coord lowerL) {
    return (base*(upperR.y-lowerL.y));
}

And then in some other function I call it this way:

Parallelogram para;   
para.areaFunc = area;

The only thing is that when I try to print it

printf("Area = %.2f", array[i].area);

(with array being the array of Parallelogram type of objects) It returns this error when compiling:

format specifies type 'double' but the argument has type 'float ()(float, Coord, Coord)' (aka 'float ()(float, struct Coordinates, struct Coordinates)') [-Werror,-Wformat] ...printf("# Area = %.2f #\n", array[I].areaFunc);

I thought I got the pointer functions right, so how can I convert the pointer function to the actual value that it should return?

2
  • 1
    Please try to create a proper minimal reproducible example that replicates the problem, and show it to us. The code you show doesn't match the error you get. And please include a full and complete copy-paste of the build output of that example. Also please refresh how to ask good questions, as well as this question checklist. Commented Jun 19, 2019 at 17:55
  • @Someprogrammerdude sorry for that... I was making one but I got the answer from Sean Bright before I could finish it. Thanks anyways Commented Jun 19, 2019 at 18:08

1 Answer 1

4

If array is an array of Parallelograms:

printf("Area = %.2f", array[i].areaFunc(array[i].base, array[i].upperR, array[i].lowerL));

Your current code is trying to print out a function pointer as a floating point number, which is obviously wrong. You need to call the function that is being pointed to.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this solved the problem. But if it's so long, what are the advantages of using the pointer functions? Isn't it the same defining a function with the struct type param and then calling it by her self?
@L_Cleo If you're always calling the same function, yes, but function pointers allow you to call different functions based on data. One could have area functions for squares and circles, too, and keep the same interface.
@NeilEdelman Ok, well then I guess I would have to make a sample program that really would see those pointer functions as more convenient than the simple function definition. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.