www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/09/11:57:46

From: "YUBS" <jdebever1 AT home DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <8UQq3.6777$K%6 DOT 145927 AT news1 DOT rdc2 DOT on DOT home DOT com> <37abee37 DOT 246073100 AT news DOT snafu DOT de>
Subject: Re: help: void pointers
Lines: 110
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Message-ID: <Lg8r3.7464$K%6.153853@news1.rdc2.on.home.com>
Date: Sun, 08 Aug 1999 05:07:55 GMT
NNTP-Posting-Host: 24.65.39.101
X-Complaints-To: abuse AT home DOT net
X-Trace: news1.rdc2.on.home.com 934088875 24.65.39.101 (Sat, 07 Aug 1999 22:07:55 PDT)
NNTP-Posting-Date: Sat, 07 Aug 1999 22:07:55 PDT
Organization: @Home Network Canada
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hey guys, thanks a lot! I was pretty messed up there, but it all makes sense
now. Thanks for showing me the light so quickly. Just another question,
Horst, you used a local helper variable "p" and cast it before using it, but
I was just wondering what the difference between casting something like
this:

pcx_picture_ptr p = image;

versus casting it like this:

(pcx_picture_ptr)p  = image;

The first way works and the second way doesn't thats all I know, but I've
always casted variables as in the second method. What's up? Is the first way
permanent and the second only for that call or what? Thanks a lot, and I
appreciate the help you've already given...

-Josh de Bever





Horst Kraemer <horst DOT kraemer AT snafu DOT de> wrote in message
news:37abee37 DOT 246073100 AT news DOT snafu DOT de...
> On Sat, 07 Aug 1999 07:04:36 GMT, "YUBS" <jdebever1 AT home DOT com> wrote:
>
> > Hi, I'm having some trouble using void pointers. I'm writing a function
that
> > will allocate enough memory for any of my types by using a void pointer
as a
> > parameter in the function. I.e.
>
> > void pcx_init (void *image, char type)
>
> > So say I have the following type:
>
>
> > typdef struct pcx_image_typ
> > {
> >     char *buffer;
> >     int width, height;
> > } pcx_image, *pcx_image_ptr
>
>
> > I want to be able to pass a pointer to a variable of type pcx_image to
> > pcx_init and have it allocate width*height memory for buffer. The
problem
> > I'm getting is that DJGPP keeps complaining that I'm "dereferencing
"void *"
> > pointer" and that "request for memeber "buffer" in something not a
structure
> > or a union".
> >
> > here's my exact code:
> >
> > void pcx_init(void *image, char type)
> > {
> >     switch (type)
> >         {
> >             case 's': {
> >                             if (!(((pcx_image_ptr)(image->buffer)) =
(char
> > * )malloc (image->width* image->height)))
> >                          }
> >         }
> > }
>
> Of course. image is a pointer to _void_ and therefore image->buffer
> etc. is meaningless.
>
> If I understand you correctly pcx_init has a void* parameter because
> it shall be used for different structure types.
>
> In this case you'd have to cast 'index' to the appropriate type every
> time _before_ you applying the -> operator to it. It's not
>
> (pcx_image_ptr)(image->buffer)
>
> which is casting the illegal expression (image->buffer) but
>
> ((pcx_image_ptr)image)->buffer
>
> which is casting 'image' and then dereferences it.
>
>
> Better readable would be a simple
>
>   case 's':
>   {
>      pcx_image_ptr p = image;
>      if ( (p->buffer=malloc(p->width * p->height))==NULL ) {/**/}
>      ...
>      break;
>   }
>
> using a local helper variable instead of the verbose and terrible
>
>
> ((pcx_image_ptr)index)->buffer =
>     malloc( ((pcx_image_ptr)index)->width *
>             ((pcx_image_ptr)index)->height
>           );
>
>
> Regards
> Horst
>


- Raw text -


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