Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm Design Manual, chapter 3, linked-list code snippet confusion

I'm reading the Algorithm Design Manual and in chapter 3, the following code snippet appears. It has to do with removing an item from a linked-list. The question isn't related to data-structures, but just to a single line of code where two variables are declared I think. I've stripped the non-relevant parts of the code for brevity.

list *search_list(list *l, item_type x) {
  // This function just searches the list x
}

list *predecessor_list(list *l, item_type x) {
  // This function simply returns the predecessor of x or NULL
}

delete_list(list **l, item_type x) {
  list *p;     /* item pointer */
  list *pred;  /* predecessor pointer */

  list *search_list(), *predecessor_list(); // What are these declarations?

  p = search_list(*l,x);

  // Code to delete the node if found is here    
}

My question is in the delete_list function, specifically, the line list *search_list(), *predecessor_list();. What is happening on that line? I'm guessing it's a pointer to a function, but my understanding is you are supposed to declare the function pointer with the appropriate parameters. Also, assuming I am correct, why are those lines even needed?

like image 993
logbaseinfinity Avatar asked Mar 19 '14 06:03

logbaseinfinity


2 Answers

The line in question,

list *search_list(), *predecessor_list();

informs the compiler that an identifier for a function exists and what its return type is. In this context, the number and type of parameter(s) the function requires is not needed.

I agree it's a bit peculiar and not very intuitive, however, the C language supports many such peculiarities.

The link provided in a comment to your question by Dabo goes into more detail: Why does an empty declaration work for definitions with int arguments but not for float arguments?

like image 134
ffhaddad Avatar answered Nov 11 '22 07:11

ffhaddad


Those are function declarations, to inform that search_list() and predecessor_list() return list*. If you use a function before you declare it, it is implicitly declared to be ‘function returning int’. In case functions search_list() and predecessor_list() defined before delete_list you won't need those declarations.

Try to put those functions after delete_list and remove declarations, you will get conflicting types for search_list() as your compiler will assume that search_list() and predecessor_list() should return int

like image 3
Dabo Avatar answered Nov 11 '22 06:11

Dabo