Article View: comp.lang.c.moderated
Article #2424Re: void* type
From: Lawrence Kirby
Date: Thu, 28 Mar 1996 00:03
Date: Thu, 28 Mar 1996 00:03
47 lines
2077 bytes
2077 bytes
In article <4ja3qf$p9b@solutions.solon.com>
hirth@wand.franken.de "Claus Hirth" writes:
>Kurt Watzka (watzka@stat.uni-muenchen.de) wrote:
>
>: yang@math.umass.edu (Huayong Yang) writes:
>
>: >void Swap(void** p1, void** p2)
>
>: It is not allowed to dereference a void *, not is it necessary in
>: this example.
>
>I agree. But you forgot to give an answer to Huayong.
>Please enlighten us with *your* solution to this problem.
There isn't a simple generic solution to this problem.
>The best I could directly think of is :
>
>void swap(void ** x, void ** y)
>{
> char * tmp;
>
> tmp = *( (char**)x );
> *( (char**)x ) = *( (char**)y );
> *( (char**)y ) = tmp;
You're treating the object being swapped as if it is a char *. If it
isn't a char * then this may not work - it results in undefined behaviour.
>}
>
>You would call it for
> SomeType * x, * y
>sth like
> swap((void**)&x,(void**)&y);
>to avoid "incompatible pointer type"-warnings.
The warnings are there to indicate that you're doing something very wrong.
Simply shutting up the warnings is shooting yourself in the foot. Pointer
casting like this is rarely a sensible thing to do.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<4jcl0p$a97@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Lawrence Kirby <fred@genesis.demon.co.uk> Newsgroups: comp.lang.c.moderated Subject: Re: void* type Date: 27 Mar 1996 18:03:37 -0600 Organization: none Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jcl0p$a97@solutions.solon.com> References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> Reply-To: fred@genesis.demon.co.uk NNTP-Posting-Host: solutions.solon.com X-NNTP-Posting-Host: genesis.demon.co.uk X-Newsreader: Demon Internet Simple News v1.27 X-Mail2News-Path: genesis.demon.co.uk