Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!decuac!cvl!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.lang.c,comp.bugs.4bsd Subject: Re: Bad Pointer Declaration in 'scandir' Message-ID: <6074@mimsy.UUCP> Date: Wed, 1-Apr-87 17:12:16 EST Article-I.D.: mimsy.6074 Posted: Wed Apr 1 17:12:16 1987 Date-Received: Sat, 4-Apr-87 06:30:46 EST References: <2479@dalcs.UUCP> <5916@mimsy.UUCP> <2491@dalcs.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 43 Keywords: scandir, C, pointers, man Xref: utgpu comp.lang.c:1416 comp.bugs.4bsd:256 >In article <5916@mimsy.UUCP> I wrote: >>>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 article <2491@dalcs.UUCP> dalegass@dalcs.UUCP (Dale Gass) writes: >... I think a pointer to an array need not specify the array size. >The compiler (all the one's I've dealt with) will allow this omission >of array size. Indeed, the declaration allows it---but just try to use the resulting variable: char (*x)[]; Now, what is the address of (*x)[0]? Well, that is easy: it is `value of x plus zero time array-size plus 0', which is of course just `value of x'. What about (*(x+1))[0]? Its address is `value of x plus 1 times array-size plus 0'. Uh oh. (The 4.3BSD C compiler never notices that anything is wrong, and merrily produces the same address as for (*x)[0].) >In many cases, it is required to be able to allow pointers to >different size arrays being passed to a function. Not so. Give an example, and I will show that you are not interpreting C's array/pointer conversion rules correctly. >struct direct ***namelist will work exactly the same as *(*namelist)[], but >the latter is a bit more intuitive (if you can call *any* C declarations >intuitive). A pointer to an array of pointers to ..... is what the beast >actually is. Scandir creates a contiguous memory region---which is not quite the same as an array---of pointers and modifies an argument to point to that region. Calling this `a pointer to an array of pointers' is misleading, because there is no real `array': An array has a size that is known at compile time. As long as you do not need the size, you can pretend one is the other. -- 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