Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Called function work at printf but not at arithmetic statement

I made a struct Triangle and I made a function to calculate p(perimeter/2).
When I call the function inside printf it works.
But when I call the same function as a simple arithmetic statement it doesn’t work and gives me following error

called object 'p' is not a function or function pointer

Source code:

#include <stdlib.h>
typedef struct{
       int a, b, c;
} Triangle;
    
int perimeter (Triangle t){
    return t.a + t.b + t.c;
}
    
float p (Triangle t){
    return perimeter(t) / 2.0;
}
    
int main() {   
    Triangle t = {3, 4, 5};
    //float p = p(t);
    printf("p = %.2f\n",p(t));
    return 0;   
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The line

float p = p(t);

defines a local variable p which shadows the global function p. This variable is of type float, so it is not a function or pointer.

Rename the variable to fix this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading