Some "weird" C codes

I have this C code, which I am having trouble 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 realized that f multiplies two variables and g multiplies, they are apparently inline. The foo function 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. These two arguments also have very strange meanings - 3392 and 3488, are these logical addresses functions sq and pl? If they are, and they are passed as integers, how does foo accept them? Starting with foo, there is a function declaration instead of the parameters to which these arguments should go.

Thanks, EDIT: cool, thanks guys a lot that cleared up!

+6
source share
4 answers

f and g not inline. They are simply parameters of the foo() function, as you can already see.

In addition, printf("sq = %d\n", sq); This behavior is undefined, since sq not an integer, but a resp function. his address in this context. Therefore you should write printf("sq = %p\n", sq); to cleanly display the address of the function.

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

So essentially foo calls pl(1,1) and stores the result in y , which is then used for sq(y,y) . Therefore, delegates are working on these features. These functions can be thought of as callback functions, since foo() calls the functions defined by the caller.

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

+4
source

Absolutely nothing special in this code. There is nothing "built-in" here.

These are regular function pointers. In declaration C

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

automatically interpreted as

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

sq and pl functions passed as arguments to foo

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

(the & operator is optional) and is 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) 

( * operator in a call is optional).

I don’t know where you got these values 3392 and 3488 . Function pointers are not passed as integers. If your debugger decided to display pointer values ​​as 3392 and 3488 , this should be a problem with your debugger.

+4
source

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 int, returns int.

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

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

You need to go and read about passing function pointers as parameters in order to get a more complete explanation of what is going on, this can help (although it is C ++) http://www.oopweb.com/CPP/Documents/FunctionPointers/ Volume / CCPP / FPT / em_fpt.html .

+2
source
 int foo(int f(int,int), int g(int,int), int x) 

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

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

 foo(sq, pl, 1) 

is correct.

+2
source

All Articles