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;
}

When you need dynamic memory, trees, lists, passing large objects to functions.

Edited 5 Years Ago by gerard4143: n/a

Another use for pointers is for passing to functions. When you pass a pointer to a function anything you do to it inside your function is retained in the calling function

void foo(int a)
{
    a += 5;
}

void bar(int * a)
{
    *a += 5;
}

int main()
{
    int a = 3;
    std::cout << "a is: " << a << std::endl;  // will be 3
    foo(a);
    std::cout << "after foo() a is: " << a << std::endl;  // will be 3
    bar(&a);
    std::cout << "after bar() a is: " << a; // will be 8
    return 0;
}

Edited 5 Years Ago by NathanOliver: n/a

Your question is:
>>When would it be practical to use a pointer to get a value instead of using the value's name?

IF you know the variable's name and have access to it, THEN you can use the variable's name instead and there is no reason to use a pointer to it instead. So the answer is NEVER.

IF you don't know the variable's name or don't have access to it, or the variable simply does not have a name (dynamic memory), THEN you have to use a pointer (or a reference) to access/modify its value.

It's not like it's really a choice (and if it is, then (almost) certainly, using the variable directly is better than using a pointer or reference to it).

It's one of those things where you have to learn the mechanics of pointers first, then later you'll understand the wisdom. One always starts from easy to hard and the easy examples are almost always examples where you could just as easily do without them, so they don't make much sense. But it HAS to start easy because you need to learn the "how" first. Eventually you'll wonder how you ever did without them.

Just don't worry about the "why" yet. Trust that there's a reason and you'll find out soon enough.

You avoid using it much as possible, instead substitute reference for it. But you might eventually need to use it when you need polymorphism, although you can still use reference, in most cases, pointers might be more practical.

Many hardware devices map real world characteristics (voltage, current, state of LED etc.) to fixed memory addresses. Accordingly, you have no option but to create a pointer that points to exactly that memory address. If you read in the manual that the value you are interested in will be put into memory address 0x22331212, the only way to get that value is with a pointer that you manually point to that address.

This question has already been answered. Start a new discussion instead.