-3

I'm trying to wrap my head around pointers in c++, what's the point of using a pointer vs just using &variable to get the object at that location?

For example(have not actually run this code, just for an example):

int score{10};
int *score_ptr {nullptr};

score_ptr = &score;

cout << "Address of score is: " << &score << endl;  // why not just this?
cout << "Address of score is: " << score_ptr << endl; // more work for same?
Tman
  • 389
  • 1
  • 14
  • 7
    It's the same thing as with any other type of variables. E.g. why would you want to use `int x = 42;`, rather than just writing `42` each time? – HolyBlackCat Jul 07 '19 at 21:48
  • You need pointers when you allocate memory in heap, for example with `new` operator. – Angelicos Phosphoros Jul 07 '19 at 21:58
  • Pointer can be NULL/nullptr, and that makes all the difference – Severin Pappadeux Jul 07 '19 at 22:05
  • 2
    Possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – Fureeish Jul 07 '19 at 23:08
  • Your example is a toy exercise that demonstrates what a pointer is, not how one would use a pointer in real code. Perhaps you just need to have patience and wait until you see more complex examples. – JaMiT Jul 08 '19 at 05:41
  • Because the next version of your program asks the user to input the number instead of just wiring in 42. The next version for pointers perhaps has an array of values you need to step through. – Gem Taylor Jul 08 '19 at 17:24

3 Answers3

0

&score is a prvalue of type "pointer to int". prvalue means "purely a value that can go on the right-hand side of an assignment statement".

int* score_ptr is an lvalue of type "pointer to int". That means it can go on the left hand side of an assignment statement.

The difference is that one is a value that has no identity, the other is a variable that has identity.

You cannot do &&score, but you can do &score_ptr. You can assign to score_ptr, you cannot assign to &score.

A pointer is akin to a piece of paper with a street address written on it. A value is a street address.

A pointer variable is a street address where there is a piece of paper saying where another street address is.

Suppose you where going moving houses every 4 months; maybe you are a coop student in a university. Giving someone your current address is a waste of time, as it will be garbage in a few months. Giving them your parent's address, and having them forward your mail to you, makes much more sense.

Here, you live in an int. If you tell someone your parent's address -- an address to a variable of type int* -- they can send stuff to you even after you move around.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

There are many reasons someone would want to use a pointer in C++

The one I would consider most fundamental is in combination with new. As an example this is undefined behavior in C++:

SomeClass* someFunc() {
    SomeClass out;
    return &out;
}

It is undefined behavior because out only has a valid memory location inside someFunc so anyone calling the function would be dealing with a bad SomeClass instance. The correct way to handle this is:

SomeClass* someFunc() {
    SomeClass* out = new SomeClass();
    return out;
}

Another reason could be in combination with nullptr. As an example say you have a bunch of people hard-coded in a program and you are implementing a phone directory. A user running the program inputs a phone number and the program either gives a pointer to the person or nullptr if the phone number doesn't belong to anyone. You would not be able to signal "belongs to no one" just using &

Another reason is manage extended classes that inherit from a common class

asimes
  • 5,749
  • 5
  • 39
  • 76
0

Pointers also help you avoid the slicing problem. For example, if you had many different classes derived from the same parent class, and you wanted to put them into a list of datatype parent

#include <vector>
class Parent{
protected:
  //variables common to all the children of the parent class
public:
  //methods common to all the children classes
}
class Child1: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
class Child2: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
std::vector<Parent*> listOfInstances;
Child1 instance1;
Child2 instance2;
listOfInstances.push_back(instance1);
listOfInstances.push_back(instance2);

if the vector called listOfInstances wasn't a pointer all the data that was unique to Child1 and Child2 would be cut off and both of those instances would become an instance of class Parent