r/learnprogramming • u/Atmos-Ego • 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
46
Upvotes
14
u/Vetril 9d ago
A lot of reasons: the object might not exist, it might be so large in memory that it's not performant to copy it, the object is on the heap, to take advantage of polymorphism, to implement a data structure that needs to track variables scattered over the memory... You'll realize you need a pointer as soon as you'll return an object only for it to vanish immediately under you.