Mail Archives: djgpp/1996/10/15/22:27:58
George Foot wrote:
> #define POINTER_SIZE sizeof(*void)
You mean sizeof(void *).
> int *my_array[20];
>
> for (int a=0;a<20;a++)
> printf("%d\n",*(my_array+a*POINTER_SIZE));
This is fundamentally wrong for several reasons.
First, pointer arithmetic doesn't work that way -- the type of the pointer
is taken into account, so my_array + i is the same as &my_array[i]; it
automatically computes the size of the pointer in question.
Second, this implicitly assumes (given your incorrect understanding of
pointer arithmetic) that all pointers are the same size, which is not only
not guaranteed by the ANSI C standard, but is in fact what the original
poster was _asking about in the first place_!
> then when you compile on a different platform the multiplier will be
> correctly adjusted.
>
> Of course, it's much simpler to write "my_array[a]", but I presume you
> don't want to do this for some reason.
The code you posted isn't even equivalent to that, much less does it
address any portability issues.
--
Erik Max Francis | max AT alcyone DOT com
Alcyone Systems | http://www.alcyone.com/max/
San Jose, California | 37 20 07 N 121 53 38 W
&tSftDotIotE | R^4: the 4th R is respect
- Raw text -