What's a C expert?

Randal Schwartz merlyn at iwarp.intel.com
Fri Aug 25 07:57:52 AEST 1989


In article <338 at mead.UUCP>, dem at 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 at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.lang.c mailing list