Mail Archives: djgpp/1996/10/12/19:27:55
In article <53oql4$qg7 AT news DOT ox DOT ac DOT uk>,
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk> wrote:
>mrichman AT cybernex DOT net wrote:
>: Is it safe to assume that all pointer variables take up the same
>: number of bytes, or is that compiler dependent.
>If you're writing really portable code, it's not safe to assume anything.
You can assume what the standard guarantees: that any pointer can be
cast to void *, and then back _to the same type of pointer it was
originally_.
>If you make a #define at the start of your source, e.g.
>#define POINTER_SIZE sizeof(*void)
You mean sizeof(void *).
>then use this, e.g.
>int *my_array[20];
>for (int a=0;a<20;a++)
> printf("%d\n",*(my_array+a*POINTER_SIZE));
This is so far off base that it's truly frightening!
First of all, pointer addition doesn't work that way. You don't need to
multiply the offset by anything:
&my_array[a] == (my_array + a)
Thus, the above is the equivalent to:
for (a = 0; a < 20; a++)
printf ("%d\n", my_array[a * POINTER_SIZE]);
Which is not (one hopes) what is desired. Not to mention the fact that
you're using "%d" to print a pointer.
Secondly, the standard does not guarantee that an int * is the same size
as a void *! All it guarantees is that an int * can be cast to a void *
and back to an int * safely. Thus, the code above wouldn't work even if
it did (if you get what I mean).
Finally, the fact that this fellow got two incorrect answers before I
came along shows why one should ask general questions about C or C++ in
comp.lang.{c,c++}, and questions about gcc in gnu.gcc.*. I don't keep
saying this because it bugs me (I'll end up reading 'em anyway); I say
it because you're more likely to get a good answer if you ask in the
appropriate forum.
- Raw text -