Pass a function-pointer name to C Preprocessor

Peter Shaw

I have a registry for functions in my C code, and I have to provide an API to register custom functions at compile time to the registry.

Example: The "customer" side of code should like:

int a_test_function_a(){
    return 13;
}
int init(void){
    add_command(a_test_function_a);
}

In my registry code I defined this macro:

#define add_command(NAME)   do{                 \
    command_registry = avl_tree_add(            \
    command_registry                            \
    , #NAME                                     \
    , &##NAME                                   \
    , &command_handler);                        \
    } while(0)

Here is what I do not understand. The output of gcc -E replace the string like expected:

do{ command_registry = avl_tree_add( command_registry , "a_test_function_a" , &a_test_function_a , &command_handler); } while(0);

But the compiler throw a error:

src/commands/commandRegisterTest.c:18:5: error: pasting formed '&a_test_function_a', an invalid
  preprocessing token
add_command(a_test_function_a);
^
src/commands/../../../src/commands/command_api.h:18:8: note: expanded from macro 'add_command'
, &##NAME                                   \
   ^
1 error generated.

How can I do this, that I do not have to call add_command('name', &function); by my own. I will register the function by it's name and just call the add_command(name) thing. Besides a solution, why is the preprocessor replace the line but fails? This doesn't make any sense for me.

ThiefMaster

Functions and function pointers are usually equivalent, i.e. you can simply omit the & ## and it'll work anyway but the preprocessor will be happy.

Anyway, you can simply remove the ## operator and it'll probably work, too. & var is fine so concatenation is not necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C pass pointer as argument in function

From Dev

Pass char pointer to function in C

From Dev

Embedding preprocessor directive into function name

From Dev

Embedding preprocessor directive into function name

From Dev

Cannot pass struct pointer as function argument in C

From Dev

Pass a pointer of an array to a function to use in printf` in C

From Dev

pass "pointer to pointer" to a template function

From Dev

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

From Dev

Pass pointer by reference to function

From Dev

Pass on function arguments by pointer

From Dev

Pass pointer to function

From Dev

Pass typedefed function pointer

From Dev

c how to evaluate function pointer using function name

From Dev

Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

From Dev

Is the C preprocessor multi-pass in operation?

From Dev

Is the C preprocessor multi-pass in operation?

From Dev

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

From Dev

C - Pass a pointer on bi-dimensional array to a function

From Dev

How to pass a pointer from C# to native function in DLL?

From Dev

String prints out junk when pass to function pointer and then back in c

From Dev

C++ How to pass member function pointer to another class?

From Dev

Is it allowed to pass a pointer to a template function to C library? (as a callback)

From Dev

c++ How to pass iterator pointer to function that expects the object by reference

From Dev

C - Pass a pointer on bi-dimensional array to a function

From Dev

Pass pointer to member function as argument with C++11

From Dev

generate a name for macro with another macro (c preprocessor)

From Dev

How can i use define preprocessor to define function-pointer?

From Dev

Pass by pointer/pass by reference in C

From Dev

How to pass in a C++ function pointer (of non-static member function) to a pre-defined C Function?

Related Related

  1. 1

    C pass pointer as argument in function

  2. 2

    Pass char pointer to function in C

  3. 3

    Embedding preprocessor directive into function name

  4. 4

    Embedding preprocessor directive into function name

  5. 5

    Cannot pass struct pointer as function argument in C

  6. 6

    Pass a pointer of an array to a function to use in printf` in C

  7. 7

    pass "pointer to pointer" to a template function

  8. 8

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

  9. 9

    Pass pointer by reference to function

  10. 10

    Pass on function arguments by pointer

  11. 11

    Pass pointer to function

  12. 12

    Pass typedefed function pointer

  13. 13

    c how to evaluate function pointer using function name

  14. 14

    Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

  15. 15

    Is the C preprocessor multi-pass in operation?

  16. 16

    Is the C preprocessor multi-pass in operation?

  17. 17

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

  18. 18

    C - Pass a pointer on bi-dimensional array to a function

  19. 19

    How to pass a pointer from C# to native function in DLL?

  20. 20

    String prints out junk when pass to function pointer and then back in c

  21. 21

    C++ How to pass member function pointer to another class?

  22. 22

    Is it allowed to pass a pointer to a template function to C library? (as a callback)

  23. 23

    c++ How to pass iterator pointer to function that expects the object by reference

  24. 24

    C - Pass a pointer on bi-dimensional array to a function

  25. 25

    Pass pointer to member function as argument with C++11

  26. 26

    generate a name for macro with another macro (c preprocessor)

  27. 27

    How can i use define preprocessor to define function-pointer?

  28. 28

    Pass by pointer/pass by reference in C

  29. 29

    How to pass in a C++ function pointer (of non-static member function) to a pre-defined C Function?

HotTag

Archive