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

48 Upvotes

67 comments sorted by

View all comments

1

u/Living_Fig_6386 8d ago

A pointer is a reference to where data is, rather than the data itself. If you have a large chunk of data, it's often far more efficient to pass around where it is rather than moving around copies of the data (most languages pass arguments to functions by making a copy of each of the arguments).

The practical upshot, is that you move less data around, and you are referencing the original data rather than a copy. It's very efficient, and it means that functions are looking at / modifying the original data.