0

My intention is to know the which function i am supposed to call based some other inputs during run time . For this i have created function pointer. Here i wrote sample code for better understanding.

#include <stdio.h>
typedef int (*ProcessFunPtr)(int a, int b);

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

int sub (int a, int b)
{
    return (a-b);
}

void check (int a, int b, ProcessFunPtr funptr)
{
    if (a<b)
    {
        funptr = &add; /*here i am expecting to copy same address in sendptr*/
        printf("%p\n", funptr);
        printf("%p\n", &add);
    }
    else
    {
        funptr = &sub;
          printf("%p\n", funptr);
        printf("%p\n", &sub);
    }
}

void calculate(int a , int b, ProcessFunPtr calptr)
{
    int c;

    c = calptr(a,b);
}
int main()
{
    ProcessFunPtr sendptr = NULL;
    int a,b;
    scanf ("%d", &a);
    scanf ("%d", &b);
    check(a,b,&sendptr); /*Here i am passing a address of function pointer*/
    if (ptr != NULL)
    {
        calculate(a,b,sendptr);
    }
    else
    {
        printf("\npointer is null");
    }
    return 0;
}

But i see sendptr is always NULL. What i need to do copy function address of add/sub to sendptr.

Cool_Binami
  • 93
  • 4
  • 12

0 Answers0