2

I am trying to create a function pointer that takes in a pointer to double as an argument. What is the proper syntax to write in main?

This is what I have but it keeps spitting out errors.

void (*ptr)(double, double, (*double), (*double) );

1 Answer 1

8

It is the same way you would declare a double * parameter in a function's parameter list:

void (* ptr) (double, double, double *, double *);

Can be assigned to a function like:

void something (double w, double x, double *py, double *pz) {
}

This is true in the general case, no matter how complicated it gets. For example, a pointer to a function that takes an int and another function pointer (which points to a function that takes a double and a void * and returns a char*) as a parameter:

void (* ptr) (int, char * (*) (double, void *));

Then:

char * g (double w, void *) {
    ...
}

void f (int x, char * (* y) (double, void *)) { 
    ...
}

// usage, elsewhere:
ptr = &f;
ptr(0, &g);
Sign up to request clarification or add additional context in comments.

1 Comment

By the way, sometimes I find it helpful to typedef types to something syntactically simpler. Then I can clearly see what's going on, and substitute the original types back in. For example, typedef double * double_ptr makes void (* ptr) (double, double, double_ptr, double_ptr) clear, then substituting double * back in shows you the correct syntax.

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.