#72_Function Pointers in C

 #include <stdio.h>


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

void func1(int (*fptr)(int, int))
{
    printf("Hello user\n");
    printf("The sum of 2 and 2 is %d\n", fptr(2, 2));
}

void func2(int (*fptr)(int, int))
{
    printf("Hello word\n");
    printf("The sum of 10 and 2 is%d\n", fptr(10, 2));
}
int main()
{

    printf("The addition of 6 and 4 is:%d\n", sum(6, 4));
    int (*fptr)(int, int);                    //declaring funtion pointer
    fptr = sum;                               //defining function pointer by pointing it to a function
    printf("The sum of %d\n", (*fptr)(4, 3)); //executing function by function pointer
    func1(fptr);                              //Passing a function pointer to another function
    func2(fptr);                              //passing same function pointer to one more function
    return 0;
}

Comments

Popular posts from this blog

#19_(2)Functions in C

Ch7_32_MethodOverloading

PS_Ch7_Q8