c how to evaluate function pointer using function name

D.Bear

This is my snippet:

typedef void (*FUNCPT)(void);

void func1();

int main(){
    FUNCPT fpt1;
    char *s = "func1";

    return 0;
}

I can evaluate fpt1 like this :

fpt1 = func1;

But there is some reason that I must use function name to evaluate function pointer, I expect to get same value by something like this:

fpt1 = (FUNCPT)s;

How can I achive this?

4386427

I don't think there is any portable way to do that. I think you need to write your own code for that. For instance a look-up table like:

#include <stdio.h>

void func1() {printf("func1\n");}
void func2() {printf("func2\n");}
void func3() {printf("func3\n");}

typedef void (*FUNCPT)(void);

typedef struct lookup
{
    FUNCPT f;
    char name[32];
} lookup;

lookup lookup_table[] = {
    {func1, "func1"},
    {func2, "func2"},
    {func3, "func3"},
};

FUNCPT getFuncByName(char* str)
{
    int i;

    for (i=0; i < (sizeof(lookup_table)/sizeof(lookup)); ++i)
    {
        if (strcmp(str, lookup_table[i].name) == 0) return lookup_table[i].f;
    }
    return NULL;
}
int main(){
    FUNCPT fpt = getFuncByName("func2");

    if (fpt) fpt();

    return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to run a function using a function pointer that located in a struct? (C)

From Dev

How to run a function using a function pointer that located in a struct? (C)

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

Using typedef function pointer in C

From Dev

Pass a function-pointer name to C Preprocessor

From Dev

how to get the function pointer to a function in C?

From Dev

Using standard function name in C

From Dev

Using standard function name in C

From Dev

When is an array name or a function name 'converted' into a pointer ? (in C)

From Dev

How to distinguish if a function pointer in C points to function 1 or function 2

From Dev

Using pointer as return value in function in c

From Dev

C++ Palindrome Bool Function (Using Pointer)

From Dev

Using PInvoke in C# with function pointer and delegate

From Dev

C function change string using pointer

From Dev

passing function pointer to the C code using cgo

From Dev

C - using to pointer to function yield no result

From Dev

C/C++: using a typedef'd function pointer to *declare* a function

From Dev

Evaluate assign function using hash

From Dev

how to declare function pointer using typedef

From Dev

How to define a multiset using a function pointer?

From Dev

How to define a multiset using a function pointer?

From Dev

C++ reserved word as function pointer name in C struct

From Dev

How to pass Enum to function and how to evaluate enum in if statement c#

From Dev

c - using a pointer returned from function in a function call

From Dev

c - using a pointer returned from function in a function call

From Dev

c++ calling function when using map of function pointer

From Dev

How to get a function name using a parent name?

From Dev

Pointer to function pointer in C++

From Dev

Using function pointer as callback

Related Related

  1. 1

    How to run a function using a function pointer that located in a struct? (C)

  2. 2

    How to run a function using a function pointer that located in a struct? (C)

  3. 3

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  4. 4

    Using typedef function pointer in C

  5. 5

    Pass a function-pointer name to C Preprocessor

  6. 6

    how to get the function pointer to a function in C?

  7. 7

    Using standard function name in C

  8. 8

    Using standard function name in C

  9. 9

    When is an array name or a function name 'converted' into a pointer ? (in C)

  10. 10

    How to distinguish if a function pointer in C points to function 1 or function 2

  11. 11

    Using pointer as return value in function in c

  12. 12

    C++ Palindrome Bool Function (Using Pointer)

  13. 13

    Using PInvoke in C# with function pointer and delegate

  14. 14

    C function change string using pointer

  15. 15

    passing function pointer to the C code using cgo

  16. 16

    C - using to pointer to function yield no result

  17. 17

    C/C++: using a typedef'd function pointer to *declare* a function

  18. 18

    Evaluate assign function using hash

  19. 19

    how to declare function pointer using typedef

  20. 20

    How to define a multiset using a function pointer?

  21. 21

    How to define a multiset using a function pointer?

  22. 22

    C++ reserved word as function pointer name in C struct

  23. 23

    How to pass Enum to function and how to evaluate enum in if statement c#

  24. 24

    c - using a pointer returned from function in a function call

  25. 25

    c - using a pointer returned from function in a function call

  26. 26

    c++ calling function when using map of function pointer

  27. 27

    How to get a function name using a parent name?

  28. 28

    Pointer to function pointer in C++

  29. 29

    Using function pointer as callback

HotTag

Archive