Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cwjcc!tut.cis.ohio-state.edu!rutgers!columbia!cubmol!ping From: ping@cubmol.BIO.COLUMBIA.EDU (Shiping Zhang) Newsgroups: comp.lang.c Subject: Re: passing *char parameters by reference Message-ID: <314@cubmol.BIO.COLUMBIA.EDU> Date: 12 Aug 89 18:45:24 GMT References: <1424@novavax.UUCP> Reply-To: ping@cubmol.UUCP (Shiping Zhang) Distribution: usa Organization: Dept. of Biology, Columbia Univ., New York, NY Lines: 34 In article <1424@novavax.UUCP> gls@novavax.UUCP (Gary Schaps) writes: >Would someone be kind enough to tell me why this program fails? > >char *x, *y; >{ > register char *temp; > > temp = x; > x = y; > y = temp; >} > >main() >{ > char *a="aaaa"; > char *b="bbbb"; > > swap( &a, &b ); > printf( " a = %s\t b = %s\n", a, b); >} The problem is the function swap. It should be defined as following: swap(x,y) char **x,**y; /* x and y are points to point to char */ { register char *temp; temp = *x; /* assign temp the address x points to */ *x = *y; /* make x point to the address y points to */ *y = temp; /* reassign y the address x used to point to */ } - ping