user1039063
user1039063

Reputation: 211

Some "weird" C code

I have this C code that i'm having problems understanding:

int foo(int f(int,int), int g(int,int), int x) {

    int y = g(x,x);
    return f(y,y);
}

int sq(int x, int y) {
    if (x == 1) { return y; }
    return pl(y, sq(x-1, y));
}

int pl(int x, int y) {
    if (x == 0) { return y; }
    return pl(x-1, y+1);
}

int main (int argc, const char * argv[])
{
   printf("sq = %d\n", sq);
   printf("output=%d\n", foo(sq, pl, 1));
   return 0;
}

I understood that f is multiplying two variables and g is multiplying, they're apparently built in. The function foo has has two parameters declared as a function declaration -> f(int, int) and g(int, int). But then foo is passed with two arguments - sq and pl. The two arguments have also very strange values - 3392 and 3488, are those logical address of the functions sq and pl? If they are and they are passed as integers, how does foo accepts them? Since foo, has function declaration in place of the parameters where these arguments should go to.

Thank you, EDIT: cool, thank you guys alot, that cleared things up!

Upvotes: 5

Views: 1096

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320391

There's absolutely nothing special about this code. There's nothing "built-in" involved here.

These are ordinary function pointers. In C declaration

int foo(int f(int,int), int g(int,int), int x)

is automatically interpreted as

int foo(int (*f)(int,int), int (*g)(int,int), int x)

Functions sq and pl are passed as arguments to foo

foo(sq, pl, 1); // same as foo(&sq, &pl, 1)

(the & operator is optional) and called through these pointers inside foo

int y = g(x,x); // same as (*g)(x,x)
return f(y,y);  // same as (*f)(y,y)

(the * operator in the call is optional).

In don't know where you got these 3392 and 3488 values. Function pointers are not "passed as integers". If your debugger decided to display pointer values as 3392 and 3488, it must be a problem with your debugger.

Upvotes: 4

Adam Straughan
Adam Straughan

Reputation: 2848

Assuming I understand your question and can remember any C then foo is a function that takes pointers to 2 functions f and g plus an int, it returns an int.

Both f and g are functions that take 2 ints and return an int.

The number you see is an address of the functions pl and sq, so it looks fine

You need to go and read about passing function pointers as parameters to get a fuller explanation of what is going on, this might help (even though it's C++) http://www.oopweb.com/CPP/Documents/FunctionPointers/Volume/CCPP/FPT/em_fpt.html.

Upvotes: 2

glglgl
glglgl

Reputation: 91017

f and g are not built in. They are just parameters of the function foo(), as you already see.

Besides, printf("sq = %d\n", sq); is undefined behaviour, as sq is not an integer value, but a function resp. its address in this context. So you should write printf("sq = %p\n", sq); in order to cleanly output the address of the function.

What really happens is that you give foo() the function sq as the parameter f and the function pl as the parameter g. foo calls these functions with the parameter x as written.

So essentially foo calls pl(1,1) and stores the result into y which is then used for sq(y,y). So it delegates work to these functions. These functions can be seen as callback functions, because foo() calls the functions given by the caller.

What sq() and pl() do is, by now, beyond my understanding.

Upvotes: 4

Daniel Fischer
Daniel Fischer

Reputation: 183858

int foo(int f(int,int), int g(int,int), int x)

declares foo as a function taking three arguments, the first two are (pointers to) functions that take two ints as arguments and return an int, the third argument is an int.

sq and pl are functions of the appropriate type, so the call

foo(sq, pl, 1)

is correct.

Upvotes: 2

Related Questions