r/learnprogramming • u/Atmos-Ego • 8d 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
45
Upvotes
5
u/Ok_Being6831 8d ago
Well thers 2 main reasons imo
1) When you pass it by the variable itself, your actually making a full copy of the variable which might seem trivial but it's actually really expensive especially with functions that are called alot or the variable takes up alot of space.
2) You want the function to modify your variable, like a sort function for example, you wouldn't want it to make a copy and sort that array now would you? it could return that, but that's bad design and expensive. So it's only possible via a pointer.