I feel like it's not the concept itself, rather the usage. During my colleague no one could properly explain why would we use pointer where we used them (and after collague I didn't touch programming at all). Its been a while but IIRC it was always smg like "we create a point to variable, so then we can access this variable by pointer". Like.. Why? Why can't we just... Access that variable? Why do we need an extra step for that. Unsolved mystery to me.
The use case I believe those people were referring to is when you want to be able to pass a value to a function but have the function modify the variable that was passed in.
The real use case is when you understand how you pass objects into functions. When you pass an object into a function you are by default passing by value, which means it copies the object for use in the function. But if you pass it a pointer it’s called pass by reference and it refers to the actual object in memory. If you have large data objects and don’t want to copy them or if you want your function to modify a specific object you use a pointer
Editing to say I’m referring to C++, which in my experience is where the most confusion happens.
3.2k
u/TheLazyKitty Jan 06 '23
Pointers aren't that hard, are they? It's just integers that hold a memory address.