Article View: comp.lang.c.moderated
Article #2419Re: void* type
From: Kurt Watzka
Date: Thu, 28 Mar 1996 00:00
Date: Thu, 28 Mar 1996 00:00
91 lines
2828 bytes
2828 bytes
Claus Hirth <hirth@wand.franken.de> 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.
>The best I could directly think of is :
>void swap(void ** x, void ** y)
>{
> char * tmp;
There is _no_ need to make this a char*. It can well be a void *, and
you don't need any casts.
> tmp = *( (char**)x );
How about
tmp = *x;
There is nothing wrong with dereferencing a pointer to pointer to void.
> *( (char**)x ) = *( (char**)y );
> *( (char**)y ) = tmp;
>From the same line of thought comes
*x = *y;
*y = tmp;
>}
>You would call it for
> SomeType * x, * y
I would _not_ call it for anything but void pointers or char pointers,
because there is no generic pointer to pointer type in C, and the only
pointer type that must have the same representation as a void pointer
is a char pointer.
>sth like
> swap((void**)&x,(void**)&y);
>to avoid "incompatible pointer type"-warnings.
Avoiding warnings in this case means that you tell the compiler you know
what you are doing. If you know what you are doing, for example, if you
know that all pointer types will have the same representation on all
machines that your program will ever run on, go ahead.
If this is not the case, you cannot write such a function using pointers
to pointers. You can still write a generic swap(), but it takes two
void pointers as it's arguments, and it has an additional "size"
parameter. Such a generic swap function can fail, and it can be used
to swap two pointers to SomeType:
#include <string.h>
int
swap(void *a, void *b, size_t sz)
{
void *tmp;
tmp = malloc(sz);
if (tmp == NULL)
return 0;
memcpy(tmp, a, sz);
memcpy(a, b, sz);
memcpy(b, tmp, sz);
return 1;
}
SomeType *a, b;
if (!swap(&a, &b))
/* die whining */
Kurt
--
| Kurt Watzka Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Technical Details
Message-ID:
<4jckqc$a5o@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: watzka@stat.uni-muenchen.de (Kurt Watzka) Newsgroups: comp.lang.c.moderated Subject: Re: void* type Date: 27 Mar 1996 18:00:12 -0600 Organization: Leibniz-Rechenzentrum, Muenchen (Germany) Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jckqc$a5o@solutions.solon.com> References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com