Path: utzoo!utgpu!watmath!iuvax!purdue!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: passing *char parameters by reference Message-ID: <19071@mimsy.UUCP> Date: 14 Aug 89 18:18:24 GMT References: <1424@novavax.UUCP> <660050@hpclwjm.HP.COM> <318@cubmol.BIO.COLUMBIA.EDU> <1010@virtech.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 54 The original question was something vague about swapping characters and/or pointers (see <1424@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@iuvax.cs.indiana.edu> bobmon@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@cubmol.BIO.COLUMBIA.EDU> ping@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@virtech.UUCP> cpcahil@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 , responds by ; it should instead do '. 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@mimsy.umd.edu Path: uunet!mimsy!chris