edited by
546 views
5 votes
5 votes

Consider the following C program:

#include<stdio.h>
int (*foo(int (*f)(int,int))) (int, int)
{
    return f+1;
}
int fun1(int x,int y)
{
    return x+y;
}
int fun2(int x,int y)
{
    return x*y;
}
int main()
{
    int (*fun_ptr[2])(int,int);
    int x,y;
    fun_ptr[0]=fun1;
    x=fun_ptr[0](4,5);
    fun_ptr[1]=fun2;
    fun_ptr[0] = foo(fun_ptr[0]);
    y=fun_ptr[0](4,5);
    printf("%d %d",x,y);
    return 0;
}

What will be the output when the above program is executed?

  1. $4\;\; 5$
  2. $20\;\; 9$
  3. $9\;\; 20$
  4. None of these
edited by

1 Answer

11 votes
11 votes

Here foo is a function which accepts a function pointer and returns its increment. But this increment value need not point to a valid function address. So, the below line can cause possible runtime error.

y=fun_ptr[0](4,5);


Correct answer: D.

edited by
Answer:

Related questions

6 votes
6 votes
1 answer
3