3

If I have a declaration like this:

int foo1 (int foo2 (int a));

How can I implement this foo1 function? Like,

int foo1 (int foo2 (int a))
{
    // How can I use foo2 here, which is the argument?
}

And how do I call the foo1 function in main? Like:

foo1(/* ??? */);
7
  • You mean you want to pass a function pointer as an argument to a function? See Function pointer as an argument Commented Mar 5, 2019 at 17:04
  • What kind of syntax is this even? It seems to compile, but it's not a function pointer. Commented Mar 5, 2019 at 17:05
  • @lurker but in my declaration, there is no a pointer. I have already see that topic. Commented Mar 5, 2019 at 17:06
  • @mkrieger1 It's a function pointer. Commented Mar 5, 2019 at 17:07
  • This compiles?? Commented Mar 5, 2019 at 17:07

3 Answers 3

8

When you declare a function parameter as a function, the compiler automatically adjusts its type to "pointer to function".

int foo1 (int foo2 (int a))

is exactly the same as

int foo1 (int (*foo2)(int a))

(This is similar to how declaring a function parameter as an array (e.g. int foo2[123]) automatically makes it a pointer instead (e.g. int *foo2).)

As for how you can use foo2: You can call it (e.g. foo2(42)) or you can dereference it (*foo2), which (as usual with functions) immediately decays back to a pointer again (which you can then call (e.g. (*foo2)(42)) or dereference again (**foo2), which immediately decays back to a pointer, which ...).

To call foo1, you need to pass it a function pointer. If you don't have an existing function pointer around, you can define a new function (outside of main), such as:

int bar(int x) {
    printf("hello from bar, called with %d\n", x);
    return 2 * x;
}

Then you can do

foo1(&bar);  // pass a pointer to bar to foo1

or equivalently

foo1(bar);  // functions automatically decay to pointers anyway
Sign up to request clarification or add additional context in comments.

3 Comments

If you have a Parameter int foo2[123] it's not exactly the same as a pointer. There is a difference with sizeof.
@harper No, there is no difference. Try it.
(This is similar to how declaring a function parameter as an array (e.g. int foo2[123]) automatically makes it a pointer instead (e.g. int *foo2).) I don't think this is a technically correct statement. Though it may appear .
0

Perhaps, this simple example could help you :

#include <stdio.h>

int foo1 (int foo2 (int),int i);
int sub_one (int);
int add_one (int);

int main() {
    int i=10,j;
    j=foo1(sub_one,i);
    printf("%d\n",j);
    j=foo1(add_one,i);
    printf("%d\n",j);
}

int sub_one (int i) {
    return i-1;
}
int add_one (int i) {
    return i+1;
}

int foo1 (int foo2 (int),int i) {
    return foo2(i);
}

Comments

-1

Have a look at the following code, it shows how to call functions they way you wanted to call.

 #include <stdio.h>


/* Declaration of foo1 . It receives a specific function pointer foo2 and an integer. */
int foo1 (int (*foo2)(int), int a);

int cube(int number)
{
    return (number * number * number);
}

int square(int number)
{
    return (number * number);
}

int foo1 (int (*foo2)(int), int a)
{
    int ret;

    /* Call the foo2 function here. */
    ret = foo2(a);

    printf("Result is: %d\r\n", ret);

    return (ret);
}

int main()
{
    int a = 3;

    foo1(square, a);
    foo1(cube, a);

    return 0;
}

2 Comments

A code dump is not an answer, especially if the code doesn't even contain int foo1 (int foo2 (int a)); or address OP's question.
@melpomene Would be please remove that vote ? Only if you think it's not wrong answer. Otherwise, let it be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.