Michael
Michael

Reputation: 13614

I have c language syntax troubels

I work on my tutorial in C language.

I have a small pice of code that I need to deciph.

Here is the code:

int  f(){return 1;}
int  g(){return 2;}
int (*h(int (*pf1)(), int (*pf2)()))(){
  return (int (*) ())((pf1() > pf2())*(int)pf1 + (pf2()>pf1())*(int)pf2);
}
void main(){
    int res = h(f,g)();
}

I have problem to unndestand this syntex:

int (*h(int (*pf1)(), int (*pf2)()))()

I can't understand what is the meaning of this outter parentheses ->(*h(int (*pf1)(), int (*pf2)()))

and this parentheses() at the end of the function's signature.

Also what is '*h' inside parentheses? I understand that it get two functions and trigger it.

Can I get brif explanation on questions above?

Upvotes: 0

Views: 134

Answers (2)

Jagawag
Jagawag

Reputation: 76

Function pointers can be tricky to read. I start by reading the inner-most parenthesis, and work my way to the outer-most parenthesis.

Here's a breakdown of that declaration:

int (*h(int (*pf1)(), int (*pf2)()))()

(*pf1)() and (*pf2)() are pointers to functions that accept no parameters (i.e. ()), and both of them return an int.

*h(int (*pf1)() , int (*pf2)()) is a function that accepts two function pointers that returnint s. So, h returns a pointer to a function that accepts nothing and returns an int

Here is a link to some examples of the syntax, and a more elaborate breakdown: https://www.cprogramming.com/tutorial/function-pointers.html


Added:

Since the original code that was provided segfaults, I wrote two different implementations that compare two integers and return the bigger one.

First, look at j. It's just a regular function that accepts two parameters that happen to be function pointers. Those function pointers are then used within the function. And because we said j returns an int, we return the actual integer by calling pf1() or pf2() with ().

Now look at h. In this scenario, h is going to return a pointer *. So we still call the function pointers pf1() and pf2() in h, but when we return, we DO NOT use () because we want to return the whole function pointer (either pf1 or pf2).

Lastly, look at main and see the difference between how j and h are used. Again, j is just a regular function. However, h uses the this syntax: h(f,g)() because h returns a pointer to a function which we then want to call using () to get the actual integer result for printing.

Hope this helps provide a little more clarity with a working example! Good luck!

#include <stdio.h>

int  f(){return 1;}
int  g(){return 2;}

int j(int (*pf1)(), int (*pf2)())
{
    return ((pf1() > pf2() ? pf1() : pf2()));
}

int (*    h( int(*pf1)() , int(*pf2)() )   ) () 
{
    return (pf1() > pf2() ? pf1 : pf2);
}


int main()
{
    int res1 = j(f,g);

    int res2 = h(f,g)();

    //print values returned by calling f and g.
    printf("f: %d \ng: %d\n", f(),g());

    //print result of calling j(f,g)
    printf("res1: %d\n", res1);

    //print result of calling h(f,g)()
    printf("res2: %d\n", res2);

    return 0;
}

Upvotes: 4

stark
stark

Reputation: 13189

main calls the function returned by h and returns that result. h compares the output of f and g and returns the function which returns the larger value (else it returns 0). Since the output of g is greater than f, h returns g. Hence main returns 2.

Upvotes: 2

Related Questions