[comp.lang.c] More on passing arrays to functions.

rbutterworth@orchid.UUCP (07/22/87)

Has anyone noticed the definition (source and man page) for
scandir(3) in BSD?

    int
scandir(dirname, namelist, select, compar)
    char *dirname;
    struct direct *(*namelist[]);
    int (*select)();
    int (*compar)();

What does "struct direct *(*namelist[]);" mean?
Well, it's an array of pointers to pointers to structures.

But what did the authors really want (the redundant parentheses
sort of indicate that they thought they were doing something else)?
You are supposed to supply the address of a pointer that will be
stuffed with the address of the newly allocated array.  So what
they needed was a pointer to a pointer to an array of structures,
i.e. "struct direct (**namelist)[];".

But of course K&R C won't let you generate pointers to arrays
in many circumstances, so what they really needed was
a pointer to a pointer to the first member of an array of structures.
i.e. "struct direct ***namelist;".

Not the same thing at all.

But C, being the friendly language that it is, silently turns the
incorrect parameter type "**namelist[]" into the correct
"***namelist".

Wasn't that nice of it?