Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!brl-adm!rutgers!husc6!panda!genrad!rep From: rep@genrad.UUCP (Pete Peterson) Newsgroups: comp.lang.c Subject: Re: pointers to arrays Message-ID: <1138@genrad.UUCP> Date: Wed, 19-Nov-86 11:21:23 EST Article-I.D.: genrad.1138 Posted: Wed Nov 19 11:21:23 1986 Date-Received: Wed, 19-Nov-86 22:24:57 EST References: <273@bms-at.UUCP> Reply-To: rep@genrad.UUCP (Pete Peterson) Organization: GenRad, Inc., Concord, Mass. Lines: 45 Keywords: What? Why? How? In article <273@bms-at.UUCP> stuart@bms-at.UUCP (Stuart D. Gathman) writes: >I am still confused about several things concerning pointers to arrays. >There does seem to be such a type, even in K & R. >1) How does one get such an animal? The only methods I can figure are > a) a cast: ( type (*)[] ) array_of_type You could declare: char *(linesptr)[81]; /*declares linesptr to be a pointer to 81 element arrays of chars. */ Or you could have: typedef char line[81]; typedef line *linept; linept linesptr; /* also declares linesptr to be a pointer to 81 element arrays of chars */ > b) a function returning such a type (but the return must use a cast!) >2) The SysV semop(2) is defined as: > --- deleted references to SysV semop --- >3) It seems to me that any distinction between a pointer to an array > and a pointer to its first element is purely semantic. (And given > the semantic difficult of obtaining a pointer to an array, why use > them?) There is no pointer conversion that I can imagine involved. > Given the declarations above, if you have: line page[20]; /* declare an array of 20 "lines" */ char *lpointer; lpointer = linesptr = page; /* point at first char of first line */ Now if you increment linesptr, you point to the next line whereas if you increment lpointer, you point to the next character; this is more than a semantic difference. The distinction is admittedly less clear when you have something like "TYPE (*pointer)[]" where the size of the array is unspecified so you can't do arithmetic on the pointers. It doesn't really seem that obtaining the pointer is that difficult. > >When are pointers to arrays appropriate? > In the example above, the difference is apparent; otherwise, it is perhaps conceptually clearer if you're dealing with arrays as units to have pointers to those units rather than their constituents. pete peterson