passing *char parameters by reference
Shiping Zhang
ping at cubmol.BIO.COLUMBIA.EDU
Sun Aug 13 04:45:24 AEST 1989
In article <1424 at novavax.UUCP> gls at 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
More information about the Comp.lang.c
mailing list