Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!tank!uwvax!astroatc!nicmad!madnix!schaut From: schaut@madnix.UUCP (Rick Schaut) Newsgroups: comp.lang.c Subject: Re: passing *char parameters by reference Message-ID: <783@madnix.UUCP> Date: 11 Aug 89 13:33:16 GMT References: <1424@novavax.UUCP> Reply-To: schaut@madnix.UUCP (Rick Schaut) Distribution: usa Organization: ARP Software, Madison, WI Lines: 44 In article <1424@novavax.UUCP> gls@novavax.UUCP (Gary Schaps) writes: >Would someone be kind enough to tell me why this program fails? [swap(x,y)] >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); >} You need to declare the parameters to swap to be pointers to pointers to chars: swap(x,y) char **x,**y; { register char *temp; temp = *x; *x = *y; *y = temp; } Now the call, swap(&a, &b) will work correctly. Remember, you're passing the pointers by reference, not the arrays, hence the extra level of indirection. -- Richard Schaut Madison, WI Madison: an alternative ArpaNet: madnix!schaut@cs.wisc.edu to reality. UseNet: ...uwvax!astroatc!nicmad!madnix!schaut {decvax!att}!