Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c,comp.bugs.4bsd Subject: Re: Bad Pointer Declaration in 'scandir' Message-ID: <5916@mimsy.UUCP> Date: Sun, 22-Mar-87 22:07:07 EST Article-I.D.: mimsy.5916 Posted: Sun Mar 22 22:07:07 1987 Date-Received: Tue, 24-Mar-87 01:45:13 EST References: <2479@dalcs.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 49 Keywords: scandir, C, pointers, man Xref: mnetor comp.lang.c:1312 comp.bugs.4bsd:244 In article <2479@dalcs.UUCP> dalegass@dalcs.UUCP (Dale Gass) writes: >I believe there is a incorrect pointer declaration in the unix 'scandir' >function (or manual page, at least) ... : > >struct direct *(*namelist[]); Well, it is certainly misleading. >According to my understanding, this should be interpreted as an array >of pointers to pointers to the direct structure.... I believe [it should >read] struct direct *(*namelist)[]; No: for there is no such thing. A declaration for a pointer to an array must specify the size of the array. In fact, what both the code and the manual *should* say is struct direct ***namelist; That is, the function wants the address of a variable declared as `struct direct **names;'. An example would help: int i, nnames; struct direct **names; extern int alphasort(); nnames = scandir("/tmp", &names, (int (*)()) NULL, alphasort); if (nnames < 0) { complain("cannot read /tmp"); return; } for (i = 0; i < nnames; i++) /* do something with names[i]->d_name */ the code for the scandir function itself reads scandir(..., namelist, ...) ... struct direct *(*namelist[]); ... The compiler, following the rules for conversion of parameter declarations, alters this to `struct direct ***namelist', which is of course correct. (No wonder it works! :-) ) Also unfortunate, incidentally, is the lack of a function to free the memory allocated by scandir(). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) UUCP: seismo!mimsy!chris ARPA/CSNet: chris@mimsy.umd.edu