0

I am conceptually confused about what I am seeing in following situation: a pointer is passed from main() to function ( func1() ) by reference. func1() then further passes this pointer to func2() also by reference. Now if func2() updates the pointer so that it now points to a new address, this reflects back in main().

Doubts:

1. Is it some sort of pointer-to-pointer-to-pointer in the background?

2. Isn't the reference *&ptr1 and *&ptr2 in func1() being initialized to NULL when ptr1 and ptr2 are passed by reference in main() ? I had read that references cannot be initialized to NULL.

#include <iostream>
using namespace std;

void func2(int *&ptr2)
{
    int var2 = 123;
    ptr2 = &var2;

    cout << endl <<"func2 - ptr2: " << ptr2 << endl;
}

void func1(int *&ptr1, int *&ptr2)
{
    int var1 = 111;
    ptr1 = &var1;
    func2(ptr2);

    cout << endl << "func1 - ptr1: " << ptr1 << "  ptr2: " << ptr2 << endl;
}

int main()
{
    int *ptr1 = NULL;
    int *ptr2 = NULL;
    cout << "main - ptr1: " << ptr1 << "  ptr2: " << ptr2 << endl;

    func1(ptr1, ptr2);
    cout << "main now - ptr1: " << ptr1 << "  ptr2: " << ptr2 << endl;

    return 0;
}

OUTPUT:

main - ptr1: 0  ptr2: 0

func2 - ptr2: 0x28fe3c

func1 - ptr1: 0x28fe6c  ptr2: 0x28fe3c
main now - ptr1: 0x28fe6c  ptr2: 0x28fe3c
learner
  • 1,197
  • 6
  • 22
  • 34
  • 2
    That is the whole point of using references. If you drop the reference & from the functions the modifications won't be visible. Indeed the ref operator can be viewed as convenience for leaving out an additional pointer. – lipp Apr 24 '16 at 16:08
  • 2
    A reference is just like another name for the object it's referring to. Like an alias. – Zaid Khan Apr 24 '16 at 16:20
  • What is the purpose of that code ? – perencia Apr 24 '16 at 17:03
  • @perencia just for understanding the pointers and references :) – learner Apr 24 '16 at 17:06

1 Answers1

3

No, there is no "pointer-to-pointer-to-pointer" in the background?.

There's just a single pointer.

Passing a reference to an object essentially passes a pointer to the referenced object. Then, if that reference is passed as a 2nd reference, the same actual, internal, pointer gets passed, as is. So the second function receives the same internal pointer than the first one.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Does this apply all the time or for function arguments only? I think i remember reading somewhere some compilers might optimize the reference variable away and replace it with the referenced variable during compilation. Not sure if that is right though. – Biruk Abebe Apr 24 '16 at 16:15
  • This applies to all references: int a; int &b=a; int &c=b; Both `b` and `c` are the same internal pointer. This is not an optional compiler optimization, of some kind. This is the only sane way to compile C++ code. – Sam Varshavchik Apr 24 '16 at 16:27
  • OK, but i meant was when the compiler sees `b` or `c` in the code,can it optimize by not using the internal pointer(for '`b` and `c`) at all and replace those occurrences of `b` and `c` by `a`. So that is not true? I think i read this [here](http://stackoverflow.com/questions/2323189/how-is-a-reference-different-from-a-pointer-in-implementation) – Biruk Abebe Apr 24 '16 at 16:34
  • This is true, of course, in this case. But, in a standalone function that receives a reference as a parameter, most of the time the compiler cannot deduce what the actual object was passed, and must generate the code that uses the reference. – Sam Varshavchik Apr 24 '16 at 16:37
  • @SamVarshavchik could you please answer my second doubt in the post? I edited it. Thanks. – learner Apr 24 '16 at 16:53
  • 1
    No the references are not initialized to NULL, themselves. References can never be null. These references are initialized to refer to pointers which are null. – Sam Varshavchik Apr 24 '16 at 16:55
  • @SamVarshavchik could you please clear my further doubts on this [link](http://stackoverflow.com/questions/36826763/pointers-passed-by-reference-updated-with-every-loop-iteration-sanity-check) – learner Apr 24 '16 at 17:51
  • You need to buy a good book on C++, and read it. This is a better way of learning C++ than by reading isolated 200-character messages on a web site. – Sam Varshavchik Apr 24 '16 at 18:49