What is function pointer?

Other Questions
1. Can we call a function indirectly?
2. Can we get address of some function?
3. Write the syntax of pointer to function
4. What is pointer to a function?

 Answer

Special pointer variable which can hold address of some function and can also call that function indirectly is called as function pointer.

Syntax
return type of target function (*ptrname) (argument list of target function);

Example
WAP having two functions add(), product() which takes two numbers as arguments and prints sum and product of given numbers.

 void add(int a, int b)
{
    printf("Sum of %d and %d is %d\n",a,b,a+b);
}
void product(int a, int b)
{
    printf("Product of %d and %d is %d\n",a,b,a*b);
}
main()
{
    void (*fp)(int,int);
    fp=&add;
    fp(5,6);
    fp=&product;
    fp(5,6);
}

Note:
Use C Visualizer for better understanding
http://pythontutor.com/c.html#mode=edit




Comments

Popular posts from this blog

What is template?

Can we access private members of an instance?

What are wrapper classes in Java?