passing *char parameters by reference

Chris Torek chris at mimsy.UUCP
Tue Aug 15 04:18:24 AEST 1989


The original question was something vague about swapping characters
and/or pointers (see <1424 at novavax.UUCP>).  In many followups, people
suggest changing swap() (whose name was half-missing from the original
posting) to take `char **' arguments, etc.

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
>>I missing something?

In article <318 at cubmol.BIO.COLUMBIA.EDU> ping at cubmol.BIO.COLUMBIA.EDU
(Shiping Zhang) writes:
>Getting rid of the ampersands would not work as intended. Only the
>first characters would be exchanged between the two strings.

and in <1010 at virtech.UUCP> cpcahil at virtech.UUCP (Conor P. Cahill) expands
on this:
>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.
[details---all correct---deleted]

This is probably correct.  However, rather like the mathematician
on the train, we have seen only half a cow.  We know only half of
statement 2: following the execution of swap(), each pointer is to
point to a string (because each is, in the original program, passed
to printf to be formatted with `%s').  We can also guess (from the
routine's name, `swap') that it is to swap something.  What we do
not know is what the writer intended to swap!  The original program
contained no comments, provided no sample output, and had no
specifications at all.

Our only remaining clue is the subject line, `passing *char parameters
by reference', and it leads me to favour the versions that both Shiping
Zhang and Conor P. Cahill suggested.  But this does bring up a point
relating to more than just C:

	You cannot debug something until you know what it is
	supposed to do.

In my experience, this is the single most troublesome aspect of all
debugging, whether for software or for hardware.  This message is, I
suppose, a plea for precision.  Do not just say: `It is broken'.  Say,
rather:  `It does this, but it should do that.'  Better yet:  `When I
<action>, <object> responds by <action>; it should instead do <action>'.
You may even find that, once you formulate the proper question, the
answer becomes obvious.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list