How to print cascaded names of functions pointers?

Viewed 235

I was working of functions pointers examples and I developed 4 simple functions and assigned them to a function pointer array, then I ran the code and worked for the 4 functions, but then I thought to also print the names of the functions.

I learned about __func__ and it only prints the name of the current function, so is there anyway to assign __func__ to the function pointer or another method to print the names of the functions?

This is the example I'm working on right now:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>


int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int divide(int x, int y);


int main() {
    int m = 6;
    int n = 10;
    int res,i;


    int (*fun[4])(int,int)={add,sub,mul,divide};

    for (i=0;i<4;i++)
    {
        printf("result of %s operation\n",__func__=fun[i]);
    }
}

int add(int x, int y) {
int result = x + y;
return result;
}

int sub(int x, int y) {
int result = x - y;
return result;
}

int mul(int x, int y) {
int result = x * y;
return result;
}

int divide(int x, int y) {
int result = x / y;
return result;
}

As you can see I'm trying to assign the __func__ to the functions pointer but of course it's not working.

4 Answers
Related