Skip to content
loading...

re: Passing a function to a structure in C VIEW POST

FULL DISCUSSION
 

Use int (*func)();

Pointer to function that returns an int

 

I want to assign the variable to the function itself...
e.g.:

structure.function()
 

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:

int
struct
char
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