Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 485
RealSlimShady Avatar asked Dec 04 '25 00:12

RealSlimShady


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*)
like image 72
dbush Avatar answered Dec 05 '25 15:12

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!