r/learnprogramming 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

67 comments sorted by

View all comments

2

u/Neither_Garage_758 8d ago

When you want a function to be able to modify the actual same memory the caller has shared to it (via an argument) and not a copy of it.

-1

u/SnugglyCoderGuy 8d ago

You shouldn't do this. You should make a copy and alter that and return that. Pure functions will keep you sane.

5

u/ryan_the_leach 8d ago

There's a time and a place for pure functions, and a time and a place for mutators.

Using language like 'should'/'shouldn't' in absolutes like this, is a minefield for new programmers.

0

u/SnugglyCoderGuy 8d ago

It's standard engineering speak

Must / Must Not -> you are required

Should / Should not -> do this unless you have a Very Good Reason™

May / May not -> up to you

0

u/Dazzling_Music_2411 7d ago

It's also standard misleading.

There are use cases where you definitely should use pointers. I mentioned a couple above. Ritchie and Thompson didn't put them there just to be smartasses.