I'm learning C++ and just finished a tutorial on pointers. I understood what they were and how to use them but I do not understand when they should be used. When would it be practical to use a pointer to get a value instead of using the value's name?
For example, why would I ever use pointers and do this:
#include <iostream>
using namespace std;
int main()
{
int x;
int *pnt_x;
pnt_x = &x;
cout << "Enter a number: ";
cin >> x;
cout << "You entered: " << *pnt_x;
return 0;
}
Instead of this, which is the same thing but much simpler.
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter a number: ";
cin >> x;
cout << "You entered: " << x;
return 0;
}