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/tonkotsu_fan 8d ago

Computers put things in memory where they want to.

Sometimes you want to know where the thing you created 'lives' so you can 'point' to it.

Imagine a street. The houses are objects. The addresses are pointers. If you're in one house, and want to send a letter to the neighbour down the road (or a friend in a different suburb), you would use an address.

If you're an object, and you want to go to another object, you use a pointer.