passing *char parameters by reference

Brian Bainter brian at trantext.UUCP
Fri Aug 11 05:17:03 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;
> {
[code to swap strings deleted]
> }
> 
> main()
> {
>    char *a="aaaa";
>    char *b="bbbb";
> 
>    swap( &a, &b );
>    printf( " a = %s\t b = %s\n", a, b);
> }



What you are trying unsuccessfully to do here, if I am not mistaken, is to
swap what a and b point to. If this is the case, then leave off the ampersands
(&) from the swap call. What you are doing wrong here is trying to pass the
address of the pointers a and b to swap instead of the addresses that a and
b point to. Remember that a and b are pointers and therefore have addresses
themselves. When you use the ampersand with a variable (pointer or otherwise)
you will be working with the address of that particular variable.



Hope this helps,

-- 

Brian R. Bainter  KA7TXA
gatech!kd4nc!trantext!brian or
gatech!tomcat!brian



More information about the Comp.lang.c mailing list