Skip to content

Passing a function to a structure in C

PDS OWNER CALIN (Calin Baenen) on January 19, 2020

I was wondering if there was any way I could pass a function to a structure in C, I've looked up my inquiry, but all I got was: How to pass a Stru... [Read Full]
markdown guide

Use int (*func)();

Pointer to function that returns an int


I want to assign the variable to the function itself...


Ok, but I need to cast a datatype, and I don't know if I made this clear (which I'm sorry about...), I was trying to ask, what datatype to I need to cast to assign something to a function, i.e. a struct member,
because the casting types, as far as I know of, are:

void // Only in the case of functions returning nothing
// Other datatypes

though you didn't provide a casting type.....

struct Test {
    int (*func)();

int tfunc() {
    return 1;

int main() {
    struct Test c = {tfunc};

    // call here
    int result = c.func();
    return 0;
code of conduct - report abuse