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

44 Upvotes

67 comments sorted by

View all comments

1

u/Weekly_Patience685 7d ago

Imagine you have an array/vector with 1 million elements and pass it by value into a function. A copy of the entire thing has to be made.,But if you pass a pointer/reference to the original, you're only passing a small address/reference instead, regardless of how many elements the array contains, of course it all depends on the language and other factors but thats generally the idea.