2

I am having problem in understanding function pointer for passing any arguments. Please find the sample program for your reference. Here I want to print the sum of two numbers. print() accepts a function pointer as its argument. But I am not able to pass the argument from main() to the function.

#include<stdio.h>

int sum(int, int);
void print(int (*p)(int,int));

int main()
{
        int a=2;
        int b=5;
        print(sum(a,b));
}

void print(int (*p)(int a,int b))
{

        printf("%d",p(a,b)); //a and b are not defined as per compiler

}

int sum(int a, int b)
{
        return a+b;
}

3 Answers 3

3

When you make this declaration

void print(int (*p)(int a,int b))

you declare a function print that takes a function pointer p that takes two ints and return an int. The names a and b do not represent anything in particular - the compiler ignores them. In fact, you could omit them:

void print(int (*p)(int,int))

If you would like to pass two numbers to be added, pass them as separate parameters:

void print(int (*p)(int,int), int a, int b)

Now your print will compile. The call will need to look like this:

print(sum, a, b);

This is because when you write print(sum(a, b)) you are instructing the compiler to call sum with parameters a and b, obtain its result, and pass to print. However, when you pass sum by itself, with no parentheses, it represents the function pointer.

Sign up to request clarification or add additional context in comments.

Comments

1

The program will look like

#include<stdio.h>

int sum( int, int );
void print( int ( * )( int, int ), int, int );
// or more simpler
void print( int ( int, int ), int, int );

int main()
{
        int a = 2;
        int b = 5;

        print( sum, a, b);
}

void print( int ( *p )( int,int ), int a, int b )
{

        printf( "%d", p( a, b ) ); //a and b are not defined as per compiler

}

int sum( int a, int b )
{
        return a + b;
}

Take into account that these declarations are equivalent and declare the same function

void print( int ( * )( int, int ), int, int );
// or more simpler
void print( int ( int, int ), int, int );

As for your original code then function print only has one parameter: a function pointer. So you may pass to function print only one argument: some function. You could define function print the following way

void print( int ( *p )( int,int ) )
{
        int a = 2, b = 5;

        printf( "%d", p( a, b ) ); //a and b are not defined as per compiler

}

Or you can define function print with three parameters that the user of the function could himself specify arguments to the called function within the body of print.

void print( int ( *p )( int,int ), int a, int b )
{

        printf( "%d", p( a, b ) ); //a and b are not defined as per compiler

}

2 Comments

So whenever function pointer is passed the argument list for the same needs to be passed separately. The job of the function pointer is to point the location of the function defined.Is my understanding correct?
@Sambaran Function print as it is declared in your code has only one parameter: a function pointer. Sp if you want to invoke a function that is passed to print as an argument you have to supply arguments to it. You could simply define arguments to the function as local variables of print. Or you can declare function print such a way that it itself would get two integers that it will pass further to the called function.
0

Let's start with a basic function which we will be pointing to:

int addInt(int n, int m) {
    return n+m;
}

First thing, lets define a pointer to a function which receives 2 ints and returns and int:

int (*functionPtr)(int,int);

Now we can safely point to our function:

functionPtr = &addInt;

Now that we have a pointer to the function, lets use it:

int sum = (*functionPtr)(2, 3); // sum == 5

Passing the pointer to another function is basically the same:

int add2to3(int (*functionPtr)(int, int)) {
    return (*functionPtr)(2, 3);
}

We can use function pointers in return values as well (try to keep up, it gets messy):

// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
    printf("Got parameter %d", n);
    int (*functionPtr)(int,int) = &addInt;
    return functionPtr;
}

But it's much nicer to use a typedef:

typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef

myFuncDef functionFactory(int n) {
    printf("Got parameter %d", n);
    myFuncDef functionPtr = &addInt;
    return functionPtr;
}

Disclaimer: this code was not necessarily compiled, if you see any errors, feel free to edit.

Comments

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.