CHANGE OF POINTERS INSIDE FUNCTIONS

Let's imagine that we want to write a function called EnterName() which have to save memory space and load the name. By doing so it is completely logical to forward the name as a parameter. But we have to ask ourselves which type must be that parameter? Transferring the pointer on sign is not going to be enough:
#include "stdafx.h"
#include "iostream"
using namespace std;
void EnterName(char *name) { // this will not work
       name = new char[100];
       cin >> name;
}
int main() {
       char *user;
       EnterName(user); // bad function call
       cout << user << endl;
       delete[] user;
       return 0;
}
If you run the previous code you'll see that the program is not running. So what’s the problem? Well as you can see the parameter are passed in all function by vale. Parameter which is inside the function call is copied into temporary variable which lives only during function execution. The parameters can be change inside the function but the change in real parameter will not affect in function main.
Function EnterName() handles with temporary values but not with contents of user variable. Address of this bloc which is allocated with operator new is not joined with local variable and on that place the string is loaded. After the function is finished the value of variable called name is lost and variable user stays unchanged.
The solution of this problem is not so complicated: instead of using pass by value procedure of user variable it is necessary to use pass the address to the function. Parameter of the function will now become the pointer on pointer on sign because the variable user is pointer on a sign but the address is pointer on pointer. Inside of function EnterName() should also take this into account, so that the value of the pointer approaches using *name. Now let's change the code:
#include "stdafx.h"
#include "iostream"
using namespace std;
void EnterName(char **name) { //The parameter is pointer on pointer
       *name = new char[100];
       cin >> *name;              //the access over the pointer
}
int main() {
       char *user;
       EnterName(&user); // bad function call
       cout << user << endl;
       delete[] user;
       system("pause");
       return 0;
}
The previous example will now work. The reason for that is: the address of variable use is passed and thus can change its contents. But the program written in this way is much more complex and becomes unreadable because:
-          In calling code which is under the function main we need to take the address and
-          In the function we have to work with operator * .
In this case the references are saving the day. Of course the pointer on the sign is some type which can be passed by value which means that we need to pass the reference on him. Here is the code which uses reference on pointer:
#include "stdafx.h"
#include "iostream"
using namespace std;
void EnterName(char *& name) {
       name = new char[100];
       cin >> name;
}

int main() {
       char *user;
       EnterName(user); // bad function call
       cout << user << endl;
       delete[] user;
       system("pause");
       return 0;

}
Share on Google Plus