Path: utzoo!attcan!utgpu!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Newsgroups: comp.lang.c Subject: Re: pointers to arrays Message-ID: <23717@watmath.waterloo.edu> Date: 17 Feb 89 14:42:53 GMT References: <19784@uflorida.cis.ufl.EDU> Organization: U of Waterloo, Ontario Lines: 52 In article <19784@uflorida.cis.ufl.EDU>, thoth@beach.cis.ufl.edu (Robert Forsman) writes: > > OK, Gordon cross wants to take the address of an array, but not in > any normal way. If E is an array (yes I know it's upper case but > that's what he used) the &E should return a pointer to an array. My > question is "what will you use this for?" Pointers are usually used > to modify things, and &E I believe would be used to modify the address > of the array since that's what E is (the address of the array). YOU > CAN'T DO THAT!!! Pointers are also used for passing things to functions. Consider: typdef char WorkBuffer[400]; void func(buf, argc, argv) WorkBuffer *buf; char **argv; { /* This function does its thing assuming that buf points * to a work buffer containing 400 bytes. */ } int main(argc, argv) char **argv; { WorkBuffer buf1; char buf2[300]; func(&buf1, argc, argv); func(&buf2, argc, argv); return 0; } Now lint, if it knows what it is doing, would complain about the second call to func() since the first argument is of the wrong type, (char (*)[300]) vs. (char (*)[400]). This is a good thing. Using the more tradition use of buffers, one would pass it to a function simply as function(buf, argc, argv), and the parameter type in the function would be (char *). If the function is something like strlen(), that is correct, but if the function is something that always expects the same length buffer, that is a bad thing. Legitimizing pointers to arrays of known size is one of the good things that the pANS has done.