Can someone help me figure this out? int * & pr

I found this on the final exam:

int a = 564; int* pa = &a; int *& pr = pa; cout << *pr; 

According to the answer to multiple choices, the code is valid and displays the value of a.

But I am confused by the rating and priority for line 3. The order of operations for C states that are * and have the same order. So, then would int *(&pr) ? How can this be described in words?

Thanks.

+4
source share
3 answers

This is a link to a pointer. In C, you would indicate this as a pointer to a pointer.

You could write something like this:

 // C++ style void update_my_ptr(int*& ptr) { ptr = new int[1024]; } // C style void update_my_ptr_c(int **ptr) { *ptr = malloc(1024 * sizeof(int)); } int main() { int *ptr; update_my_ptr(ptr); // Here ptr is allocated! } 
+3
source

The third line defines a link to a pointer (or a link to a pointer if you want). Assigning it to a pointer makes pr actually be an alias of pa , and when evaluating it, it indicates where pa pointing, i.e. a .

In the variable declaration, * and & have no operators value, therefore, priority does not make sense here.

+5
source

Line 3 creates a link (read: alias) to an int pointer. If you set pr to 0, pa also be 0 (and vice versa).

+1
source

All Articles