How do I print the cascading names of function pointers?

I worked on examples of function pointers and I developed 4 simple functions and assigned them to an array of function pointers, then I ran the code and worked for 4 functions, but then I thought of printing the function names as well.

I found out about __func__

and it only prints the name of the current function, so is there anyway to assign to __func__

a function pointer or other method to print function names?

Here's an example I'm currently working on:

#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 __func__

to a function pointer, but of course it doesn't work.

+3


source to share


4 answers


The constant __func__

in each function is only available at runtime. This means that if you want to use it, you must capture it when you call the function. Like this:

typedef int calc_func_t (int x, int y, const char** func);

int add(int x, int y, const char** func);
...

calc_func_t* fun[4] = {add,sub,mul,divide};

for (i=0;i<4;i++)
{
    const char* func;
    int result = fun[i](1,1,&func);
    printf("result of %s operation %d\n", func, result);
}

...

int add(int x, int y, const char** func) 
{
  int result = x + y;
  *func = __func__;
  return result;
}

...

      



If you want to know what functions are named at compile time and then use that information, the simplest and best way would be to create a lookup table:

typedef struct
{
  calc_func_t* func;
  const char*  name;
} calc_func_info_t;

const calc_func_info_t func_info [] =
{
  {add, "add"},
  {sub, "sub"},
  ...
};

      

+3


source


The compiled code does not contain function names and symbols by default. You can use some macros to create constant strings containing function names to be included in the compiled binary:



#include <stdio.h>

// function call type
typedef void fn_call();

// record describing one function
typedef struct fn_record {

    const char *name; // function name as a constant string
    fn_call *call; // function call

} fn_record;

// the functions to be called and named
void fna() { printf("called fna\n"); }
void fnb() { printf("called fnb\n"); }
void fnc() { printf("called fnc\n"); }

// macro, that generates record for each function, it creates 
// string like { "fna", fna } to save You typing
#define FN_RECORD(f) { #f, f }

// now define array of functions records
fn_record fns[3] = {
    FN_RECORD(fna),
    FN_RECORD(fnb),
    FN_RECORD(fnc)
};

// ... which becomes:
// fn_record fns[3] = {
//     { "fna", fna },
//     { "fnb", fnb },
//     { "fnc", fnc }
// };


int main(void) {

    int i;

    // ... use it whatever You like
    for (i = 0; i < 3; i++) {

        printf("%s\n", fns[i].name);
        fns[i].call();
    }

    return 0;
}

      

+2


source


Basically, there is no way.

// A function declaration
void MyFunction(void);

// a pointer to the function
void (*fnPointer)(void) = MyFunction;

// No way to retrieve function name "MyFunction" from fnPointer

      

Of course, if you have a known set of possible functions that can be assigned to a pointer, you can explicitly compare them.

Alternatively, you can change the stored function pointer to be concatenated with the function name. It could be something similar to the following:

struct FunctionPointerContainer
{
    void (*fnPointer)(void);
    char* fnName;
};

#define MakeFunctionPointer(fn) FunctionPointerContainer { fn, #fn }

// later
struct FunctionPointerContainer myPointer = MakeFunctionPointer(MyFunction);
myPointer.fnPointer(); // call
myPointer.fnName; // the function name

      

0


source


I have been working on examples of function pointers

So, as I understand it, your example is for educational purposes , isn't it? In this case, I would make it as simple as it can be, so as not to confuse the students who are using your example to learn how function pointers work.

I found out about __func__

and only prints the name of the current function

Even though the other answers show very nice and clever solutions on how to have a function name outside of it, in terms of simplicity , I would still use __func__

, as it usually does, inside a called function:

#include <stdio.h>
#define print_info(format, ...) \
    printf("Function '%s' is called with parameters: "format"\n", \
    __func__, __VA_ARGS__)

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, n = 10, i;    
    int (*fun[4])(int,int) = { add, sub, mul, divide };

    for (i = 0; i < 4; ++i)
        printf("-- result is: %d\n", fun[i](n, m));

    return 0;
}

int add(int x, int y) {
    print_info("%d, %d", x, y);
    return x + y;
}

int sub(int x, int y) {
    print_info("%d, %d", x, y);
    return x - y;
}

int mul(int x, int y) {
    print_info("%d, %d", x, y);
    return x * y;
}

int divide(int x, int y) {
    print_info("%d, %d", x, y);
    return x / y;
}

      

0


source







All Articles