Another silly question

Jeff Erickson krazy at claris.com
Thu Apr 27 19:23:38 AEST 1989


>From article <2459 at nmtsun.nmt.edu>, by kelly at nmtsun.nmt.edu (Sean Kelly):
> He expects that many compilers would instead add the value of i to the
> pointer a, and then reference the item stored there.  I say that the
> compiler's smart enough to realize what we're trying to achieve, and
> won't do something like "* (char *) ((int) a+i)" which he thinks it
> will probably do on most machines.

You're right.  You're instructor is full of donkey doo-doo.

In fact, since a[i] = *(a+i), and (a+i)=(i+a), you can actually write i[a]
for a[i] and most compilers will take it!  (Every one I've tried has, anyway.)

I refer you to page 205 of K&R, second edition.

	"A pointer to an object in an array and a value of any integral
	type may be added.  The latter is converted to an address
	offset by multiplying it by the size of the object to which
	the pointer points.  The sum is the same type as the original
	pointer, and points to another object in the same array,
	appropriately offset from the original object.  Thus, if P
	is a pointer to an object in an array, the expression P+1 is
	a pointer to the next object in the array."

If I were you, I'd question your instructor's qualifications to his superior.
This is one of *the* most useful features of C.  He obviously isn't well-
versed in the language he's trying to teach you.    ~~~~~~~~~
-- 
Jeff Erickson       Claris Corporation  | Birdie, birdie, in the sky,
408/987-7309      Applelink: Erickson4  |   Why'd you do that in my eye?
krazy at claris.com     ames!claris!krazy  | I won't fret, and I won't cry.
       "I'm a heppy, heppy ket!"        |   I'm just glad that cows don't fly.



More information about the Comp.lang.c mailing list