C++ double pointer assignment

358 Views Asked by At

Is there any difference between the following two assignment methods?

int * ptr1;
int ** ptr2;

//method 1
*ptr2 = ptr1;
//method 2
ptr2 = &ptr1;

1

There are 1 best solutions below

0
On
int* ptr; //declare a pointer
int** ptr2; //declare a pointer that will point to another pointer

*ptr2 = ptr; //dereferencing invalid address, undefined behaviour
ptr2 = &ptr; //makes ptr2 point to ptr

They are not the same!