Article View: comp.lang.c.moderated
Article #2456Re: void* type
From: David McReynolds
Date: Tue, 02 Apr 1996 01:55
Date: Tue, 02 Apr 1996 01:55
67 lines
1828 bytes
1828 bytes
Perhaps I'm confused but when I read the question this is what I came
up with. It compiles and worked as expected in a test case.
As far as the two pointers to be swapped are concerned, they are of
the same type correct?
I do not see the problem with:
void swap_ptr(void **p1, void **p2) {
void *tmp = *p1;
*p1 = *p2;
*p2 = tmp;
return;
}
Granted I did not do an extensive test but I did try ints, chars and floats
with no surprises. As in:
#include <stdio.h>
#include"swap_ptr.h"
int main(void) {
char c1='A', c2='B', *pc1, *pc2;
int i1=1, i2=2, *pi1, *pi2;
float f1=1.1, f2=2.2, *pf1, *pf2;
pc1 = &c1;
pc2 = &c2;
pi1 = &i1;
pi2 = &i2;
pf1 = &f1;
pf2 = &f2;
printf("%c , %c\n", *pc1, *pc2);
swap_ptr(pc1, pc2);
printf("%c , %c\n", *pc1, *pc2);
printf("%d, %d\n", *pi1, *pi2);
swap_ptr(pi1, pi2);
printf("%d, %d\n", *pi1, *pi2);
printf("%f, %f\n", *pf1, *pf2);
swap_ptr(pf1, pf2);
printf("%f, %f\n", *pf1, *pf2);
return(0);
}
The output looks like:
r980(davidm)$ main
A , B
B , A
1, 2
2, 1
1.100000, 2.200000
2.200000, 1.100000
r980(davidm)$
Could someone enlighten me as to why this thread is so long?
Thanks
Dave
Technical Details
Message-ID:
<4jq1e4$a2g@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: davidm@ssi.stratus.com (David McReynolds) Newsgroups: comp.lang.c.moderated Subject: Re: void* type Date: 1 Apr 1996 19:55:16 -0600 Organization: Stratus Computer Inc, Marlboro MA Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jq1e4$a2g@solutions.solon.com> References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> Reply-To: David_McReynolds@vos.stratus.com NNTP-Posting-Host: solutions.solon.com X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]