delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/24/05:26:50

Message-Id: <199906240926.LAA22742@deimos.worldonline.nl>
From: "Bart Alewijnse" <scarfman AT geocities DOT com>
To: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il>
Cc: <djgpp AT delorie DOT com>
Subject: Re: Dereferencing a void pointer
Date: Thu, 24 Jun 1999 11:24:40 +0200
X-MSMail-Priority: Normal
X-Priority: 3
X-Mailer: Microsoft Internet Mail 4.70.1155
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019