Mail Archives: djgpp/1999/06/24/05:26:50
> On 23 Jun 1999, Bart Alewijnse wrote:
>
> > I have a struct with a 'void *data' member, of which the type I assign
when
> > I judge what data goes in it. The compiler yaks at me when I try to
offset it
> > like:
> >
> > to->data[offset]=0;
>
> The compiler `yaks' at you because it cannot generate code for
> `data[offset]' without knowing the size of `data'. For example, if
> `data' points to an int, then each unit in `offset' increments the
> pointer by 4, if it's a short, it increments the pointer by 2, etc.
>
> A void pointer doesn't say anything about the size of the datum it
> points to, so the compiler is helpless. To solve this, cast the data
> to the correct type before dereferencing.
I knew that I needed to cast it. I didn''t know the exact syntax, though.
Eg. '(char *)to->data[offset]=0;' (my first try) didn't work, nor did some
other complicated-looking casts. I already got another reply, telling me
that '((char *)to->data)[offset]=0;' was the correct format.. Thanx
anyways.
-Bart
- Raw text -