Search code examples
cfunctionfunction-declaration

Function declaration within a function definition with wrong signature?


I have encountered the following code:

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

    p = search_list(*l,x);
    if (p != NULL) {
        pred = predecessor_list(*l,x);
        if (pred == NULL) /* splice out out list */
            *l = p->next;
        else
            pred->next = p->next;
        free(p); /* free memory used by node */
    }
}

I couldn't quite understand what this line means:

    list *search_list(), *predecessor_list();

Is this a function declaration? If yes;

  1. Why is there a function declaration within a function? I thought that function declarations are supposed to be outside of all functions.
  2. Why is the signature (parameters) of the declarations do not match how these functions are called in the code?

If this is not a function declaration, then what is this?


Solution

  • Yes it's a couple of function prototype declarations.

    And as any declaration you can have them inside a function. It's function definitions (i.e. their implementation) you can't have inside other functions.

    As for the argument mismatch, that's how C works. If you don't declare any arguments then the function will be declared to have an unknown number of arguments of unknown type, and you can really call them passing just about anything and the compiler won't know if it's right or wrong.