How to declare function pointer in header and c-file?

后端 未结 3 1167
清歌不尽
清歌不尽 2020-12-31 02:12

I\'m a little confused over how to declare a function pointer in a header file. I want to use it in main and a file called menus.c and declare it in menus.h I assume. We wan

3条回答
  •  轮回少年
    2020-12-31 02:44

    A function pointer is still a pointer, meaning it's still a variable.

    If you want a variable to be visible from several source files, the simplest solution is to declare it extern in a header, with the definition elsewhere.

    In a header:

    extern void (*current_menu)(int);
    

    In one source file:

    void (*current_menu)(int) = &the_func_i_want;
    

提交回复
热议问题