Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!munnari!otc!mikem From: mikem@otc.OZ (Michael Mowbray) Newsgroups: comp.lang.c++ Subject: Re: Passing pointers by reference Message-ID: <49@otc.OZ> Date: Mon, 1-Dec-86 18:07:51 EST Article-I.D.: otc.49 Posted: Mon Dec 1 18:07:51 1986 Date-Received: Tue, 2-Dec-86 08:20:00 EST References: <572@sdcc18.ucsd.EDU> Organization: O.T.C. Systems Development, Australia Lines: 48 In article <572@sdcc18.ucsd.EDU>, ee161aba@sdcc18.ucsd.EDU (David L. Smith) writes: > foo(bar & * junk) > > This would be a pointer to bar passed by reference to the > function foo. No it's not. You've made junk a pointer to a reference to a bar, whereas you wanted a reference to a pointer to a bar. If you use foo(bar* &junk) it works. (This sort of error always catches people. To understand declarations you have to read them backwards, i.e: right to left.) Example follows: //------------------------------------------------------------ #include struct Bar { int i; }; Bar b1,b2; void foo(Bar* &junk) { junk = &b2; } main() { cout << "&b1,&b2: " << (long)&b1 << " " << (long)&b2 << "\n"; Bar* bp = &b1; cout << "bp before foo: " << (long)bp << "\n"; foo(bp); cout << "bp after foo: " << (long)bp << "\n"; } //------------------------------------------------------------ $ a.out &b1,&b2: 16960 16964 bp before foo: 16960 bp after foo: 16964