Path: utzoo!attcan!uunet!wuarchive!brutus.cs.uiuc.edu!tut.cis.ohio-state.edu!cs.utexas.edu!usc!apple!oliveb!mipos3!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.c Subject: Re: What's a C expert? Message-ID: <4847@omepd.UUCP> Date: 24 Aug 89 21:57:52 GMT References: <12214@well.UUCP> <6057@microsoft.UUCP> <1336@atanasoff.cs.iastate.edu> <338@mead.UUCP> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: all Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 43 In-reply-to: dem@mead.UUCP (Dave Myers) In article <338@mead.UUCP>, dem@mead (Dave Myers) writes: [...] | Hmmm.... I don't claim to be a C expert Obviously not, because.... :-) | , but let's take a trivial | case. An array of anything with a size different from that of int | will do. | | int i; | struct element { | int x[BIGNUMBER]; | } a[WHATEVER]; | | a[i] == *(a + (i * sizeof(element))) | i[a] == *(i + (a * sizeof(int))) Both of these lines are wrong. a[i] = *(a + i) since 'a' is a pointer, and 'i' is an integer, 'a+i' is the same as executing a++ for 'i' times (or a-- for '-i' times if i<0). i[a] = *(i + a) since 'a' is a pointer, [this will sound familiar], and 'i' is an integer, 'i+a' is the same as executing a++ for 'i' times (or a-- for '-i' times if i<0). See, it's the same thing! The scaling happens during the "addition" (which isn't really addition) of a pointer and an integer BY DEFINITION. This definition just happens to make the array "operator" commutative. Just another longtime C hacker, (and no, I don't write i[a], but I could! :-) -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel, Hillsboro, Oregon, USA | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/