Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some "weird" C code

Tags:

c

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!

like image 220
user1039063 Avatar asked Dec 10 '12 22:12

user1039063


People also ask

Is C hard to code?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What makes C unsafe?

C and C++ are unsafe in a strong sense: executing an erroneous operation causes the entire program to be meaningless, as opposed to just the erroneous operation having an unpredictable result. In these languages erroneous operations are said to have undefined behavior.

Can I code C in Mobile?

Android is based on Linux Kernel so it's definitely possible to compile & run C/C++ programs on Android. C is quite cross-platform , so a C Program written in Windows can Run on Linux ( and android ) and vice versa.


2 Answers

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.

like image 72
glglgl Avatar answered Oct 06 '22 00:10

glglgl


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.

like image 45
AnT Avatar answered Oct 06 '22 00:10

AnT