Path: csiph.com!news.swapon.de!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 12:38:52 -0700 Organization: None to speak of Lines: 71 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="30520"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tVjQalxojH8YN9qx+nU9Y" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:+mUebbYcs1aTUWNd6Xq6hxFge/Q= sha1:dJuEzUrxVvxXDQQrtZffNHxD/VQ= Xref: csiph.com comp.lang.c:92148 BartC writes: > On 11/08/2016 17:17, Keith Thompson wrote: >> 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. > > This is one of those things that is very easy to change. Or to prohibit > in this case. (It could even be a compiler warning.) No, it wouldn't have been very easy to change. It would have broken existing code, something that the maintainers of the language try very hard to avoid doing. Sure, it could have been done -- and if it had been my decision, and I had thought of it at the time, I might have advocated for such a change. But the benefits of banning integer+pointer, and therefore integer[pointer] and integer[array], just aren't that significant. It would make *some* currently valid code invalid (and I'll bet you that some compilers would continue to support it as an extension). Fixing such code would not be particularly difficult -- if you ignore the sometimes substantial overhead of making *any* change to existing code. The costs would probably be smaller than those imposed by removing implicit int in C99 -- but the benefits would also be smaller. Removing implicit int allowed for detection of real errors in existing code. I don't think reversing an array and an index is something that's often done accidentally. Finally, some people have argued that being able to write index[array] is actually useful. I'm not convinced myself, but it's something to consider. There is no programming language in which it's the least bit difficult to write bad code. Banning integer+pointer, integer[pointer], and integer[array] would remove one rarely used way to write arguably bad code. It's just not worth the effort. It would make sense to do it as part of a general overhaul of the language, but the result of such an overhaul should almost certainly be considered a new language and called something other than "C". > You compile a program, and if it complains about offset+pointer, then > you fix it, or compile in compatibility mode if it's someone else's code > that you can't change. > > This should however have been taken care of decades ago. Why is it so important to you? [...] -- 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"