Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!mrsvr.UUCP!kohli@gemed.med.ge.com From: kohli@gemed (Software Surfer) Newsgroups: comp.lang.c Subject: Re: "abcdef"[3] == 3["abcdef"], but why? Keywords: rtK&R pointers arrays Message-ID: <1081@mrsvr.UUCP> Date: 29 Sep 89 18:16:16 GMT References: <781@cc.helsinki.fi> Sender: news@mrsvr.UUCP Reply-To: kohli@gemed.med.ge.com (Software Surfer) Organization: GE Medical (Applied Science Lab) Lines: 20 On 29 Sep 89 15:29:00 GMT, Marko Teittinen, student of computer science, wrote: >Could someone explain to me what a C compiler does when it runs into >expression 3["abcdef"]? > Look out-- this was posted earlier this year as one of several feeble tests of C wizardry, so you'll get a lot of postings. The reason 3["abcdef"] is equivalent to "abcdef"[3] is that "abcdef" is a char*, and 3 is an int. Expressions of the form: ptr[offset] are evaluated as ptr+offset, which, you'll note is equivalent to offset+ptr, which is what you'd expect from offset[ptr]. Jim Kohli