2

Hi I've been looking over stackoverflow and I'm really struggling with function pointers as parameters.

I have the structure:

 struct Node { struct Node *next; short len; char data[6]; }; 

and the functions:

 void selectionsort(int (*compareData)(struct Node *, struct Node *), void (*swapData)(struct Node *, struct Node *), int n); compare(struct Node *a, struct Node *b); swap(struct Node *a, struct Node *b); 

selection sort serves only to call compare and swap:

 void selectionsort(int compare(struct Node *a, struct Node *b), void swap(struct Node *a, struct Node *b), int n){ int i; for (i = 0; i < n; i++){ compare(a, b); swap(a, b); } } 

(the contents above may be incorrect, I have yet to get to fooling around with the actual selectionsort function really).

The problem arises when I call selectionsort in main. I was under the impression this would work:

 int main(int argc, char **argv){ int n; struct Node *list = NULL; for (n = 1; n < argc; n++) list = prepend(list, argv[n]); //separate function not given here. int (*compareData)(struct Node *, struct Node *) = compare; //not sure I needed to redeclare this void (*swapData)(struct Node *, struct Node *) = swap; selectionsort(compareData(list, list->next), swapData(list, list->next), argc); //other stuff return 0; } 

Note: the function prepend contains the malloc for the struct, so that's been dealt with.

The problem I'm having is no matter how I play around with my function declarations etc, I always get the following error:

warning: passing argument 1 of 'selectionsort' makes pointer from integer without a cast [enabled by default] note: expected 'int (*)(struct Node *, struct Node *)' but argument is of type 'int'. 

Any help explaining why I'm getting this error message and how to fix it would be immensely appreciated.

I understand that the function is expecting a function pointer but I assumed my code above would allow for compareto be called. Any input would be greatly appreciated, also this is for an assignment (so please help me avoid cheating) and the parameters int(*compareData)(struct Node *, struct Node *) void (*swapData)(struct Node *, struct Node *) were given.

2 Answers 2

3

In

selectionsort(compareData(list, list->next), swapData(list, list->next), argc); 

you are passing the result of calling the function compareData. This is wrong.

The same holds for swapData.

Just pass the function itself (resp. a pointer to it):

selectionsort(compareData, swapData, argc); 
Sign up to request clarification or add additional context in comments.

Comments

2

In your call to selectionsort you are actually calling those function pointers, causing you to pass the result of those calls to selectionsort. They are normal variables, and should be passed to selectionsort like any other variable argument.

However, you don't actually need the variables, you can just pass the functions directly:

selectionsort(&compare, &swap, argc); 

Note that the address-of operator is not strictly needed, but I prefer to use them to explicitly tell the reader that we are passing pointers to those functions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.