4
#include<stdio.h>

int add(int i,int j)
{
        printf("\n%s\n",__FUNCTION__);

        return (i*j);
}

int (*fp)(int,int);

void main()
{
        int j=2;
        int i=5;

        printf("\n%s\n",__FUNCTION__);

        fp=add;

        printf("\n%d\n",(*fp)(2,5));
        printf("\n%s\n",*fp);
}
4

1 回答 1

4

You can compare the function pointer with a pointer to function. Like this :

    if (fp==add)
        printf("\nadd\n");

There are no other (standard) ways1.

This

printf("\n%s\n",*fp);

is a compilation error.


There are platform specific ways. For linux, this works :

#include<stdio.h>
#include <execinfo.h>

int add(int i,int j)

{

        printf("\n%s\n",__FUNCTION__);

        return (i*j);

}

int (*fp)(int,int);

union
{
    int (*fp)(int,int);
    void* fp1;
} fpt;

int main()
{
        fp=add;


        fpt.fp=fp;

        char ** funName = backtrace_symbols(&fpt.fp1, 1);
        printf("%s\n",*funName);
}
于 2013-09-18T06:08:40.647 回答