[comp.lang.c++] Passing pointers by reference

ee161aba@sdcc18.ucsd.EDU (David L. Smith) (12/01/86)

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 attempted to fool the
pre-processor by doing:

class bar {
	typdef bar *bar_p;
	foo(bar_p & junk)
}

This fools the pre-processor, but the code it produces chokes the C compiler.
Specifically, it omits the typedef for bar_p and attempts to use it anyhow 
without any kind of definition anywhere.

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

mikem@otc.OZ (Michael Mowbray) (12/02/86)

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 <stream.h>

    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

bs@alice.UUCP (12/02/86)

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.