www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/08/11:52:14

From: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: help: void pointers
Date: Sun, 08 Aug 1999 10:04:52 GMT
Organization: [Posted via] Interactive Networx
Lines: 102
Message-ID: <37ad3f4e.77764316@news.snafu.de>
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> <Lg8r3.7464$K%6 DOT 153853 AT news1 DOT rdc2 DOT on DOT home DOT com>
NNTP-Posting-Host: n242-180.berlin.snafu.de
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sun, 08 Aug 1999 05:07:55 GMT, "YUBS" <jdebever1 AT home DOT com> wrote:

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

(note: image is a void*)
 
> 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...

You should get a precise understanding of what "casting" really
means/is in C.

If you apply a cast like

  (int)(x) or (void*)(x) or (pcx_picture_ptr)(x)

to the expression 'x', you are converting the _value_ of the
expression 'x' to a _value_ of type 'int' or 'void*' or
'pcx_picture_ptr'. Note that the result of a cast is always a _value_
_only_ like a number and never a "variable", i.e. you can never assign
anything to an expression of the form '(type)x'. Its like trying to
assign
	
		1 = 5;

In the first example

	pcx_picture_ptr p = image;

you are defining a variable of type 'pcx_picture_ptr' and initializing
with the void* 'image'. It has the same effect as

	pcx_picture_ptr p;
	p = image;

Hidden in this assignment is an implicit cast done by the compiler,
because a value of type (void*) can be implicitly converted in C to
any pointer type. In C++ you would have to say explicitly:

	pcx_picture_ptr p = (pcx_picture_ptr)image;

or

	pcx_picture_ptr p;
	p = (pcx_picture_ptr)image;


Note that here (in C implicitly) the _value_ in 'image' is taken,
converted to an pcx_picture_ptr and the converted _value_ is assigned
to p.

Now you may use expressions like

	p->buffer = ...

Of course you could to this "on the fly". Instead of creating a
temporary variable to which the converted value of 'image' is stored
once and for all you could do this conversion always explicitly
without a helper variable:

	((pcx_image_ptr)image)->buffer = ...

In fact the permanent conversion to a separate variable only relieves
you from the task of writing all the time

	((pcx_image_ptr)image)


It's just like creating a temporary variable in this case

	s = a + b + c;

	x = s/a ; y = s/b ; z = s/c

instead of writing


	x = (a+b+c)/a ; y = (a+b+c)/b ; z = (a+b+c)/c;

In our case the permanent conversion to p doesn't really save
execution time because the conversion from void* to pcx_image_ptr in
fact does "nothing" - it only gives the correct type to the pointer.
Thus the version with the permanent p will probably generate more code
in the executable (some nanoseconds because of the permanent
assignment) - but it makes the code better readable.


Regards
Horst

- Raw text -


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