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?

6
  • 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." Commented May 9, 2022 at 8:27
  • stackoverflow.com/questions/840501/…, stackoverflow.com/questions/14114749/c-function-pointer-syntax Commented May 9, 2022 at 8:28
  • 1
    @WhozCraig int, int, int, *int ? Commented May 9, 2022 at 8:28
  • 1
    @WhozCraig the last int should have an asterisk attached. Commented May 9, 2022 at 8: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? Commented May 9, 2022 at 8:31

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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
@jrswgtr I still think that the answer is correct in the first, dense form. Anyway, it is extended now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.