Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Pointers, constant values Date: Fri, 16 Oct 2015 13:01:11 -0700 Organization: None to speak of Lines: 50 Message-ID: References: <5b2ab074-7644-4798-9b90-a3ae47e1f2b8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="5441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/s2Fh4cFVJnJ5ijl3hPr8i" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:xaFYS4+uM6KYpq1qe9gF6jcU4t4= sha1:m2gcW0XUTJ+WpgG8k6hC3sU0MlE= Xref: csiph.com comp.lang.c:73064 Bartc writes: > On 16/10/2015 20:13, Keith Thompson wrote: >> Bartc writes: > >>> int array_sum(int (*array)[], int n); >> >> Yes, but there's not much you can do with it, since you haven't >> specified the length of the array it points to. (I'm a little surprised >> that that declaration is legal.) > > It's a pointer to an unbounded array. You can do anything you like with > it except anything involving its length. But int* doesn't have a length > either. No, but int* specifies the size of the object it points to. Given int (*array)[], you don't even know the size of the element type. > Actually, requiring a bound to be specified would severely restrict it > as you it would then only work for arrays of that length. Although you > might of course want that behaviour: if you implement a 3D point as > float (*)[3], you don't want anything passing other other kind of array. > > >>> However this also now requires proper array dereferencing and >>> indexing, not just applying an offset to an arbitrary pointer which >>> can be to anything: >>> >>> (*array)[i] >> >> That won't compile unless you have something between the [] in the >> declaration. > > It works because I've tried it. (I wouldn't use this style of parameter > passing if writing actual C, it's too fiddly, but my generated code > always uses proper pointer-to-arrays.) $ cat c.c int array_sum(int (*array)[], int n) { array[0]; } $ gcc -c c.c c.c: In function 'array_sum': c.c:2: error: invalid use of array with unspecified bounds $ -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"