Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Difference between these pointer syntaxes Date: Thu, 11 Aug 2016 09:17:25 -0700 Organization: None to speak of Lines: 43 Message-ID: References: <8737mdh47k.fsf@bsb.me.uk> <87mvkkflc0.fsf@bsb.me.uk> <871t1wf8f8.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="19935"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19P+LevhuUFAjEl7lRMdqNM" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:BaJwO/LU0P2gJQyGf3gTd80sgLU= sha1:qFIj+uocalEq6m2D+h47rJHxfjk= Xref: csiph.com comp.lang.c:92127 BartC writes: [...] > Let me ask you, do you think it was a good idea for + between pointer > and offset to be commutative? > > That is, allowing offset+pointer as well as pointer+offset. The question wasn't directed at me, but I'll answer it anyway. No, I don't think it's a good idea. It was probably sensible in the ancient pre-K&R1 version of C in which integers and pointers were largely interchangeable, and certainly in B where I don't think there was any real distinction between integers and pointers. There was no good opportunity to change the language to make offset+pointer, and therefore offset[pointer], illegal. Such a change would have broken existing code (though likely not much of it), and would not have had much corresponding benefit. I've discussed it in my answer here: http://stackoverflow.com/a/18393343/827263 > Because, allowing the same equivalence between a[i] and *(a+i), that > allows someone to write i[a]. Even though hardly anyone does this, you > can never be quite sure. You can never be quite sure of what? > It also makes nonsense of the syntax a[i]: in a million other languages > the thing in brackets is the index or selector, the thing outside is the > collection or sequence you're trying to index into. > > So a[i] in C is obviously intended to look like those other uses, but > that becomes futile is someone can also write i[a]. Or, worse, when i is > the array and a is the index, and they write a[i]. So don't do that. -- 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"