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.