passing *char parameters by reference

Conor P. Cahill cpcahil at virtech.UUCP
Mon Aug 14 09:54:44 AEST 1989


In article <24555 at iuvax.cs.indiana.edu>, bobmon at iuvax.cs.indiana.edu (RAMontante) writes:
> Among all the responses to this swap question, I think I saw _one_
> that suggested getting rid of the ampersands and passing in the array
> pointers (instead of the addresses of the array pointers).  All the
> others leap right in and rewrite the swap routine to accept the
> addresses of the pointers, at the expense of working through an
> additional, unneeded level of indirection.
> 
> Do people have some objection to quick and straightforward fixes, or am
> I missing something?

You are missing something.  The problem is
	
	1.  There are 2 pointers to character strings in the main routine
	2.  Following the execution each pointer is to point to the other
	    string.

Passing in the address of the pointer is the ONLY way.  

Another mechanism would be to copy the data to a temp space, copy over the 
first pointer, and copy from the temp space to the second pointer.  This 
would require that the data areas pointed to by both pointers is writeable 
(constant strings are not always writeable) AND is the same size.

Consider the following:

	char	* a = "01234567890";
	char	* b = "0";

The only mechanism that could swap what these pointers point to would be
to modify the pointers themselves.



More information about the Comp.lang.c mailing list