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?

L_Cleo
  • 1,073
  • 1
  • 10
  • 26
  • 1
    Please try to create a proper [mcve] 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](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jun 19 '19 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 – L_Cleo Jun 19 '19 at 18:08

1 Answers1

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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 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 Jun 19 '19 at 18:09
  • 2
    @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. – Neil Jun 19 '19 at 18:56
  • @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 – L_Cleo Jun 19 '19 at 22:29
  • See https://stackoverflow.com/questions/12971995/how-pointers-to-function-as-struct-member-useful-in-c and https://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c and the Wikipedia article has some examples, https://en.wikipedia.org/wiki/Function_pointer. – Neil Jun 23 '19 at 19:50