'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?
Solution 1:[1]
This is how you deal with function pointers in a manner that will keep you sane:
Write a
typedefsimilar to the function declaration you want. In this case it's just about addingtypedefin 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
typedefas a function template.To use a function pointer to this function type, simply do
sequence_t* ptr;.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lundin |
| Solution 2 |
