1

I'm trying to create code which uses pointers to functions as parameteres, and I have to use a typedef. I'm not a C pro. It feels like I'm getting there, but I can't seem to find a good explanation of the syntax of pointers to function.

I have a function fillArray:

long fillArray(long *array, int x, int y) {
//
}

then I want to make a typedef of a pointer to this function:

typedef long (*fArray)(long, int, int);

fArray pFill = fillArray;

and I want to give this pFill to a function called doThis():

int doThis (fArray pFill) {
  return 0;
}

and calling it using:

int y = doThis(pFill);

What am I doing wrong?

0

2 Answers 2

5

Your typedef needs to be:

typedef long (*fArray)(long *, int, int);
                            ^
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Kinda lame that I didn't see this myself. Thanks!
2

Your fillArray function accepts a long * as first parameter, you forgot a * into the typedef:

typedef long (*fArray)(long *, int, int);

Comments

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.