anon88
anon88

Reputation: 1

passing function to another function multiple variables c

void generate_sequence (int xs, int currentlen, int seqlen, int *seq);
void check_loop_iterative(void (*f)(?), int xs, int seqlen, int *loop, int *looplen);

I need to pass first function to second function.So my question is.How should I fill the parameters where the question mark is?

Upvotes: 0

Views: 59

Answers (2)

the busybee
the busybee

Reputation: 12600

How should I fill the parameters where the question mark is?

You write the same list as the pointed function has:

void generate_sequence (int xs, int currentlen, int seqlen, int *seq);

void check_loop_iterative(void (*f)(int /*xs*/, int /*currentlen*/, int /*seqlen*/, int* /*seq*/), int xs, int seqlen, int *loop, int *looplen);

The parameter names are not necessary, so I put them in comments.

Upvotes: -2

Lundin
Lundin

Reputation: 213842

This is how you deal with function pointers in a manner that will keep you sane:

  • Write a typedef similar to the function declaration you want. In this case it's just about adding typedef in front and coming up with a meaningful type name:

    typedef void sequence_t (int xs, int currentlen, int seqlen, int *seq);
    

    It doesn't matter what you name the parameters to and you don't even need to name them, though you should ideally have all functions of this type using the same parameter names. So regard the typedef as a function template.

  • To use a function pointer to this function type, simply do sequence_t* ptr;.

Upvotes: 4

Related Questions