2

Lets say I have an extern function pointer in a header file

extern void (__stdcall * const glEnable) (GLenum cap);

is there a way to define it inside a function, because its definition depends on other stuff

void glInit()
{
    ...
    Lib lib = loadLib("opengl");
    glEnable = procAddr(lib, "glEnable");
    ...
}
3
  • 1
    to define it inside a function No it is not possible - symbols declared at file scope have to be defined at file scope. I do not believe such an answer solves your real underlying problem in any way... are you asking an XY question? Commented Jun 1, 2021 at 20:16
  • Are you aware of the difference of declaring, defining and initialising a variable? Commented Jun 1, 2021 at 20:22
  • It's unclear what initialization needs to be done on a function pointer. Could you show us how you'd like this to work so we can see the "other stuff"? Commented Jun 1, 2021 at 20:24

1 Answer 1

1

You can do this:

// .h file
extern void (__stdcall * glEnable) (GLenum cap);


// .c file
void (__stdcall * glEnable) (GLenum cap) = NULL;

void glInit()
{
    Lib lib = loadLib("opengl");
    glEnable = procAddr(lib, "glEnable");
}

Now, the function pointer is null until someone calls glInit. It is undefined behavior to call that function pointer before calling glInit, and I assume that was your intention anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.