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 12:13:11 -0700 Organization: None to speak of Lines: 56 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="25043"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rFeOuizM5geQeyKP+1gyJ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:xyzSf7gI26IwBCc23HBVykNzz88= sha1:NELUdrBNMD1Rvt4XbhVxSilU2uA= Xref: csiph.com comp.lang.c:73058 Bartc writes: > On 16/10/2015 16:25, Keith Thompson wrote: >> Alla _ writes: >> [...] >>> int array_sum (int array[], const int n); >> >> One piece of advice: >> >> The parameter declaration >> int array[] >> really means >> int *array >> >> There is no such thing as an array parameter in C, but it uses >> array-like syntax to define pointer variables. >> >> My advice is *not* to take advantage of that special-case rule. It's a >> pointer, so define it as a pointer: >> >> int array_sum (int *array, const int n); > >> Not everyone will agree with this; some will argue that using the [] >> notation is a reminder that it points to an element of an array, not >> just a single int object. > > But 'int *array' *is* a pointer to a single element. Yes. Isn't that pretty much what I said? It's a pointer to an int object; using array notation suggests that that object is the initial element of an array. (Then again, for at least some purposes *any* single object can be treated as an element of a 1-element array.) > C allows you to > pass a pointer to an actual array, if not the array itself: > > 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.) > 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. -- 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"