Function Pointers

Both are types. The difference is:

  • zig function bodies are compile time symbols that are resolved by the loader to an address
  • zig function pointers are runtime pointers to the starting address of the function

C function pointers map to zig function pointers, it is the same concept.
Zig function bodies are a comptime concept; the address of the function is not known at comptime because it is decided by the loader at run time.


Zig Language Reference / Functions

There is a difference between a function body and a function pointer. Function bodies are comptime-only types while function Pointers may be runtime-known.


EDIT: Note that “function pointers may be runtime-known”, this is the most confusing part, it means that some times zig treats function pointers as comptime function bodies
e.g. this inline for works

    const funcs = [_]*const fn () void{ foo, bar };
    inline for (funcs) |func| {
        func();
    }
4 Likes