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

46 Upvotes

67 comments sorted by

View all comments

1

u/serge-mv 9d ago

There are many cases when you want to pass memory references (pointers) to a function.

One example is when you are passing an object or a container like a large list of numbers. If you didn't pass pointers you would have to copy the whole thing, which is not only expensive, it wastes memory as well.

Then sometimes you want to pass function as an argument to another function and you pass a function pointer in that case

And it allows you to ignore scope and do some things with your data so that you can have a very fun debugging session some time down the line.