piclist 2008\07\23\162147a >
Thread: Starting with C
picon face BY : Tomás Ó hÉilidheemail (remove spam text)

Michael Rigby-Jones wrote:
> object[index] is NOT equivalent to *(object+index) unless you happen to be only using objects that are the same size as the data width of the target.

It is indeed equivalent, in exactly the same way as "5 + 2" is
equivalent to "2 + 5".

When you write arr[5], it's exactly the same as writing *(arr + 5).
Therefore there's no reason why you shouldn't be able to write *(5 +
arr), which is why you can write 5[arr] instead of arr[5].

> index[object] instead of object[index] is likewise doomed to failure unless sizeof(index) == sizeof(object[0])
> Off the top of my head *(object + ( index * sizeof(object[0]))) would seem to be equivalent to an array index.

You're forgetting something about pointer arithmetic in C:  When you do
arithmetic with a pointer, the arithmetic is based on the size of the
type. For instance:

   int *p = &whatever;

   p += 4;  /* This advances the pointer by four times the size of an
int */

   double *p = &something_other_variable;

   p += 4;  /* This advances the pointer by four times the size of a
double */

So, to summarise, arr[5] is equal to 5[arr] in every conceivable
context, with every kind of type.

seeking: what about compiler
<488792B9.1080004@lavabit.com> 7bit

In reply to: <BAC9B53D3316E9419514CB5DB2AF275D385146@pai-smx-01.BOOKHAM.ENTERPRISE.PRI>
See also: www.piclist.com/techref/microchip/languages.htm?key=c
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Starting with C

month overview.

new search...