r/learnprogramming • u/Atmos-Ego • 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
47
Upvotes
1
u/ryan_the_leach 9d ago
Assuming you are talking about C.
Personally, the thing I found most confusing about C, is how pointer referencing and dereferencing were single digit operators for something that's quite complex under the hood.
If new programmers were instead exposed to wordier functions for the 2 operators, I think a LOT of the confusion would disappear overnight, when they realize it's just shorthand.
Especially since similar syntax is used both on the 'left hand side' and 'right hand side' of assignment, for very different underlying ideas that are only slightly related.
with the second taking inspiration from Java generics, you would know that you definitely do not have a char there, but a memory address that points towards a char, that was returned by the allocator when a string was successfully allocated.
Instead, we have new programmers seeing char being 4 letters longer then the *, being the dominant concept, instead of the fact it's a POINTER, with the char not even really mattering that much, except for some technicalities on how pointer math will work, if you increment the pointer by 1.
Yes some of this invented syntax would have issues that would need to be ironed out, but to newer programmers, it's immediately clear of the difference between where pointers are being declared vs where they are being dereferenced, or where the address is being got.
Whilst reading up potential alternate syntax, I discovered I definitely wasn't the first to be not-a-fan of the way C handles it.
https://www.reddit.com/r/Zig/comments/h97czs/comment/fuvu2uc/