Is this a correct way to pass a function into a struct?

Viewed 90

I looked but couldn't find a direct reference for this question. I'm new to function pointers (and C), so I don't know all the tricks one can do yet :)

I've effectively got a function:

void select_comparator(My_Struct *structure, int (*comp)(int x, int y)) {
    ...

...where My_Struct has prototype:

typedef struct my_struct {
    int (*comp)(int x, int y);
} My_Struct;

Modulo some minor details. I just want to know if the following is correct syntax:

void select_comparator(My_Struct *structure, int (*comp)(int x, int y)) {
    structure->comp = comp;
}

It seems almost too easy, and I'm worried it is.

2 Answers
Related