🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2485

Re: void* type

#2485
From: Mike Rubenstein
Date: Wed, 03 Apr 1996 13:28
81 lines
2704 bytes
davidm@ssi.stratus.com (David McReynolds) wrote:

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

Perhaps it's so long because people keep posting nonsense like you
just did.

If that program compiles, you need to get another compiler.  swap_ptr
is defined as taking void** arguments but your program is passing
char*, int*, and float* arguments.

Unless, of course, you are lying to the compiler in swap_ptr.h and
telling it that the function takes void* arguments.  In that case the
program results in undefined behavior.  In practice most compilers
will swap more memory than they should (for some types), possible
messing up other variables or less than they should, messing up the
variables being swapped.

Michael M Rubenstein
Technical Details
Message-ID: <4jtud1$3ug@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jq1e4$a2g@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: miker3@ix.netcom.com (Mike Rubenstein)
Newsgroups: comp.lang.c.moderated
Subject: Re: void* type
Date: 3 Apr 1996 07:28:01 -0600
Organization: Netcom
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jtud1$3ug@solutions.solon.com>
References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jq1e4$a2g@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-NETCOM-Date: Tue Apr 02  7:59:49 AM CST 1996
X-Newsreader: Forte Agent .99d/32.182