Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!princeton!allegra!alice!bs From: bs@alice.UUCP Newsgroups: comp.lang.c++ Subject: Re: Passing pointers by reference Message-ID: <6412@alice.uUCp> Date: Tue, 2-Dec-86 08:58:05 EST Article-I.D.: alice.6412 Posted: Tue Dec 2 08:58:05 1986 Date-Received: Tue, 2-Dec-86 22:06:05 EST Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 46 Summary: it works, but beware of the declarator syntax In article <572@sdcc18.ucsd.EDU>, ee161aba@sdcc18.UUCP writes: > > Is it possible to call a function with a pointer by reference? What I am > trying to do is pass a pointer and I want to be able to modify the pointer > value (not what it points to). Logically, something like: > > foo(bar & * junk) > > should do it. This would be a pointer to bar passed by reference to the > function foo. However, this does not work. ... > I realize I could use a double indirected pointer (and I probably will end > up doing so) but I'm lazy and the call by reference is an elegant mechanism. > Now if only it would work! > > David L. Smith > UC San Diego > sdcsvax!sdcc18!ee161aba I think you simply got the declaration wrong foo (bar&*); declares a pointer to a reference, not a reference to a pointer. How about: f(int*& p) { p++; // change the pointer value } main() { int i; int* a = &i; printf("%d\n",a); // some value f(a); printf("%d\n",a); // some value + sizeof(int*) } Yes, the declarator syntax is perverse. Try reading them right to left (that is easier than the proper ``inside out'' and often sufficient to get them right): int*&: reference to pointer to int int&*: pointer to refernce to int const int *: pointer to integer constant int *const: constant pointer to integer etc.