0
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?

anon88
  • 1
  • 2
  • 1
    `int,int,int,int*` spring to mind. That, going off the soothsaying assumption that *"I need to pass first function to second function."* actually means "I need to declare a function pointer parameter `f`, compatible with the first function, as the first parameter to the second function." – WhozCraig May 09 '22 at 08:27
  • https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work, https://stackoverflow.com/questions/14114749/c-function-pointer-syntax – ForceBru May 09 '22 at 08:28
  • 1
    @WhozCraig `int, int, int, *int` ? – Yunnosch May 09 '22 at 08:28
  • 1
    @WhozCraig the last int should have an asterisk attached. – bipll May 09 '22 at 08:28
  • check_loop_iterative(generate_sequence(xs,curr_len,seqlen,seq),seqlen,loop,looplen); I m calliing function like this but it gives an error where is my fault? – anon88 May 09 '22 at 08:31
  • @anon88 `generate_sequence(xs,curr_len,seqlen,seq)` calls the function, rather than passing on the function pointer. It should be `check_loop_iterative(generate_sequence,seqlen,loop,looplen);` and then call generate_sequence through the function pointer later inside `check_loop_iterative`. – Lundin May 09 '22 at 08:34

2 Answers2

4

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;.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-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.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/31727629) – jrswgtr May 09 '22 at 13:24
  • @jrswgtr I still think that the answer is correct in the first, dense form. Anyway, it is extended now. – the busybee May 09 '22 at 14:17