daljinder singh
daljinder singh

Reputation: 187

Reference to a pointer

I am just a little confused between two statements.

1.

    int a = 42;
    int *p = &a; //declares pointer p to a
    int &r = *p; //this is not the way to declare a reference to a pointer, but what does this statement do

To print value of, it can be done by

    cout << a << *p << r;

All the above will print the value of a, but how, that is what i want to know.

  1. Now this is how a reference to a pointer is defined

    int i = 42;
    int *p;
    int *&r = p; //declares reference r to pointer p
    r = &i; //stores the address of i in pointer p
    

I just want to understand why the first one doesn't define a reference to a pointer.

Upvotes: 1

Views: 96

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

In this code snippet

int a = 42;
int *p = &a; //declares pointer p to a
int &r = *p; //this is not the way to declare a reference to a pointer, but what does this 

the expression *p yields lvalue of the object a because the pointer p points to the object. So this declaration

int &r = *p;

declares a reference to the same object a using an indirect access to the object through the pointer p.

From the C++ Standard (5.3.1 Unary operators)

1 The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T”, the type of the result is “T”. [ Note: Indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. —end note ]

The difference between the two code snippets presented in the question is that in the first code snippet there is declared a reference to an object of type int (int a = 42; ) using indirection by means of a pointer. While in the second code snippet there is declared a reference to a pointer (int *p;).

Upvotes: 5

lets break it down (from Right to Left):

int &r = *p; 

this

*p  

de -references the pointer

and this

int& r

is the reference declaration

at the end

int& r = *p;

is equivalent to

int& r = a;

Example:

int main()
{
    int a = 42;
    int *p = &a; //declares pointer p to a
    int &r = *p;

    cout << "a: " << a << endl;
    cout << "r: " << r << endl;

    cout << "changing a:" << endl;
    a = 17;
    cout << "a: " << a << endl;
    cout << "r: " << r << endl;
    cout << "changing r:" << endl;

    r = 0;
    cout << "a: " << a << endl;
    cout << "r: " << r << endl;

    cin.get();
    return 0;
}

Upvotes: 4

KjMag
KjMag

Reputation: 2770

That is because in the first case you've just declared a regular reference to int and you assigned *p to it, which is an int. *p is not a pointer, but the value that the pointer p points to - in this case it's just the value of a.

Upvotes: 0

Related Questions