Is there a difference ... between ptr** and ptr*& ...
Yes. The former is a pointer, to an object of type ptr*
, while the latter is a reference to an object of type ptr*
.
or preferred ...
void change(int** ptr) {
**ptr = 50;
*ptr = nullptr;
}
void change(int*& ptr) {
*ptr = 50;
ptr = nullptr;
}
Advantages of a reference
You'll find that the reference is implicitly indirected, which makes the syntax simpler and that usually helps readability. This is particularly important in case of reference / pointer to a pointer. Same advantage applies to getting a reference: You don't need to use the address-of operator.
Another advantage is that a reference cannot be reassigned to refer to another object. This makes it simpler to read programs that use references, since you don't need to figure out whether a reference is reassigned within a long algorithm; You know that it isn't.
Another advantage is that a reference cannot be null. Therefore you don't need to check for such eventuality within the function. If you pass null to the first example function, the behaviour would be undefined, which is a very bad thing.
Disadvantages of a reference
You'll find that the reference is implicitly indirected. This can be confusing to people who are familiar with only value types (C programmers).
Another disadvantage is that a reference cannot be reassigned to refer to another object. This appears to not be a problem for your function since you don't need to refer to more than one object, but in general, there are situations where it would be useful.
Another disadvantage is that a reference cannot be null. This appears to not be a problem for your function which is presumably never intended to handle such case. But in general, there are situations where you want to represent a referential relation to non-existing object.
Preferences are personal, but in my opinion, the advantages of the reference outweigh the disadvantages, except when you need one of the features that are not possible (nullability, re-assignment)