Starting with C
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, 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.
> index[object] instead of object[index] is likewise doomed to failure unless sizeof(index) == sizeof(object)
> Off the top of my head *(object + ( index * sizeof(object))) 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
double *p = &something_other_variable;
p += 4; /* This advances the pointer by four times the size of a
So, to summarise, arr is equal to 5[arr] in every conceivable
context, with every kind of type.
seeking: what about compiler
In reply to: <BAC9B53D3316E9419514CB5DB2AF275D385146@pai-smx-01.BOOKHAM.ENTERPRISE.PRI>
See also: www.piclist.com/techref/microchip/languages.htm?key=c
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the