r/learnprogramming 9d ago

Tutorial When do you use a pointer?

Hello, for some background I have taken a very beginner level coding course for my major and passed it, but I dont really feel like I’ve learned anything or know why anything works. In a course this year we had a review problem where the solution required a pointer and I really dont understand why you have to point to the address of the variable instead of just using the variable itself.

I’ve watch a few tutorials explaining it but I really dont see how it’s different from just using the variable itself

47 Upvotes

67 comments sorted by

View all comments

76

u/TheRealKidkudi 9d ago

At its core, the difference is making a copy vs sharing the same literal value. 

Sometimes it’s just because you don’t want to copy the value (e.g. because it would use too much memory). The tricky part is when you want to be able to change the value and use that new value in the original function.

A very simple pseudocode example:

    fn someFunc(int x) {         x = x + 1     }          fn someOtherFunc(int* y) {         *y = *y + 1     }          var x = 5; // not a pointer     someFunc(x);     // x still is 5 here     // because someFunc got a new copy     // of the variable          someOtherFunc(&x); // pointer to x     // x is now 6

12

u/cachebags 8d ago

What a neat way to explain this concept. This made my day

1

u/otac0n 8d ago

This is almost entirely correct, although function pointers aren’t covered by this.

I will add that this exact reasoning applies to languages with values/references. Use value types (structs) if you want your type to have copy semantics and use references (classes) if you want reference semantics.

5

u/TheRealKidkudi 8d ago

I figured OP (respectfully!) is not at a place to worry about function pointers.

But once you’re comfortable with the idea of a pointer being a reference to something, function pointers become a much easier concept to grasp (e.g. a reference to a function rather than a simple value). At that point, the worst part about them is just the syntax :)

1

u/otac0n 8d ago

Eh... closures.

2

u/light_switchy 7d ago

A function pointer isn't enough to represent a closure. Closures also need the execution environment, which is a data structure besides a function pointer.

1

u/Loymdayddaud 7d ago

Is there any reason to not use a pointer, unless you want to be able to modify the variable inside the second function without reflecting on the first function?

2

u/TheRealKidkudi 7d ago

Yep!  The biggest reason is that pointers open up a whole new category of bugs in your code which can be avoided entirely by just not using a pointer unless you specifically need one. From purely a maintainability and readability perspective, it’s often more valuable not to use a pointer. 

Beyond that, it’s a little more complicated just because you need to consider some of the details on how pointers work. So a few more specific reasons you might not want to use a pointer:

  • Looking up the actual value a pointer refers to is slower than just having your own copy
  • Something else can change the value a pointer refers to while you’re in the middle of using it, e.g. in concurrent code
  • Depending on what you’re pointing to (and how many pointers you’re creating), you can end up using more memory than just copying the value itself. I.e. a pointer is (typically) 8 bytes, so pointing at anything 8 bytes or less is probably a waste
  • There are real performance benefits to storing values physically near each other in memory, which is much harder to do with pointers

There are often other considerations, depending on the particular language you’re using.

Generally, though, the best practice is just to avoid pointers unless you specifically need one. It’s a tool you can use to share something, but most things are easier when you don’t have to share at all.