Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: passing *char parameters by reference Message-ID: <1010@virtech.UUCP> Date: 13 Aug 89 23:54:44 GMT References: <1424@novavax.UUCP> <660050@hpclwjm.HP.COM> <24555@iuvax.cs.indiana.edu> Organization: Virtual Technologies Inc Lines: 31 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 > 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.