How to return a pointer from a function that calls a function by pointer to a function that returns a pointer?

Viewed 47
node *insertPlaceOrder(node *head, char *firstName, char *lastName, int day, int month, 
    int year, char *birthPlace) {
  //CODE
return head;                     
}

node *insertToList(node (*(*order))(node*, char*, char*, int, int, int, char*), node *head, 
    char *firstName, char *lastName, int day, int month, int year, char *birthPlace) {
  return (*order)(head, firstName, lastName, day, month, year, birthPlace);
}

When I debug this code, the compiler gives me the following error:

incompatible types when returning type 'node {aka struct node}' but 'node * {aka struct node *}' was expected.

How can I get the insertToList() function to return the pointer to a struct node that the function insertPlaceOrder() returns?

1 Answers

You have an extra set of parenthesis in the function pointer type. You have:

node (*(*order))(node*, char*, char*, int, int, int, char*)

Which defines order to be a pointer-to-pointer-to-function returning a node, not a pointer-to-function returning a node *. It should instead be:

node *(*order)(node*, char*, char*, int, int, int, char*)
Related