passing *char parameters by reference

Conor P. Cahill cpcahil at virtech.UUCP
Thu Aug 10 12:02:35 AEST 1989


In article <1424 at novavax.UUCP>, gls at novavax.UUCP (Gary Schaps) writes:

I assume there is a line here that says:
  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);
> }


1.  You are calling swap with the & (address of) a character pointer.
    This is a pointer to a pointer.  Therefore the swap function must
    be written as follows:

	void
	swap(x,y)
		char **x;
		char **y;
	{
		retister char *temp;
		temp = *x;
		*x = *y;
		*y = temp;
	}
	



More information about the Comp.lang.c mailing list