+ 14

What are pointers used for in real life?

Time for a noob question. I know what pointers are and how they are used. But I'm still struggling with when I'm supposed to use pointers instead of "regular" variables. E.g. almost every tutorial teaches you how to use pointers by using them for really basic things like adding two integers, a and b. Instead of adding a and b directly, two pointers *pa and *pb are created and the values they are pointing to are added. Fine. I understand that this is for educational purposes and you probably wouldn't use pointers for a basic task like that in real life. But then again StackOverflow seems to agree that you shouldn't use pointers if you can do without them. My problem is that I can hardly think of any situations where you can't use regular variables instead of pointers. In fact several programming languages don't even have pointers, so I'm sure there's always a work-around? Let's say I have a function that changes a variable. I want to change the original variable, so usually I would pass the variable by reference (using a pointer). But if I want to follow StackOverflow's advice and avoid using pointers, I could just pass the variable by value, change it in the function and store the return value in the original variable? So, no pointer needed here? Am I wrong? Am I missing something? Is this all about efficiency, memory management, stack vs heap etc.?

27th Sep 2018, 10:20 AM
Anna
Anna - avatar
14 Answers
+ 12
Even languages like Python and Java use pointers as part of their class implementation. Pointer are crucial to programming real life code. However, using the hidden pointers in classes is a much safer coding practice than playing with them yourself. Using reference variable parameters is much safer than passing a pointer. You want to limit your pointer usage as much as possible as the bugs they cause are difficult to find. However, understanding pointers and being able to safely use them is a very important concept. It gives you knowledge of what the compiler is doing for you in languages like Java. It allows you to make specialized optimizations needed for time critical code where the safe method isn't fast enough.
27th Sep 2018, 3:52 PM
John Wells
John Wells - avatar
+ 9
Thank you all for your answers and code samples. It's really cool that you can ask weird questions here and get actually helpful answers without being yelled at 😅 Many platforms can learn a lot from Sololearn 😊👍
27th Sep 2018, 5:08 PM
Anna
Anna - avatar
+ 7
Right, in my studies the lecturers also said to avoid pointers if possible. But in some situations it is really benefitial. Taking your example to copy by value... This is totally fine. However, if you have a very big user-defined object, it is better to only pass a pointer to this object and operate on the original than copying all of this huge object. Then again there are structures like linked lists, that need to know the position of the next list for example, so storing a pointer to it instead of a copy of the whole lists. It always depends on your needs. Bottomline is: there are pointers for a reason and not totally unnecessary. They can improve runtime and also make things a bit easier, even if for beginners it might look over complicated, because of presented easy teaching examples.
27th Sep 2018, 11:07 AM
Matthias
Matthias - avatar
+ 7
I have a class example coded in both C and C++. The C version gives what the underlying compiler generated code for the C++ version would look like. Any language's class implementation would look similar to the C code. https://code.sololearn.com/cg2MNAExxvQ6 https://code.sololearn.com/c373AHt3O2jy Before C++ existed, that C code was how OOP was done. The same kind of code was created in many non OOP languages because OOP was known to be important long before any language supported it.
27th Sep 2018, 4:07 PM
John Wells
John Wells - avatar
27th Sep 2018, 10:27 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 5
Pointers are used to use Virtual and secondary storage. I know you have to go very deep to understand my above line. May be following example can help you Example : If you regular variables that means at run time you require a exact memory size in your device to store information. But if you use pointer that means it only needs an address not value to hold information further when you need the value you can call the address to get values. Thanks
28th Sep 2018, 5:13 PM
Jai Verma
Jai Verma - avatar
+ 3
Knowing Pointers also helped me to understand some strange behaviour of Python, like this: https://code.sololearn.com/cfWIQqYIPwT1/?ref=app
27th Sep 2018, 4:24 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 3
pointers are put simply analogous to our home address.. if you are asked where do you stay.. you tell your address..
30th Sep 2018, 5:39 AM
Ssa.Sak
Ssa.Sak - avatar
+ 2
Ssa.Sak Thank you. A part of me knew that making the question more than one sentence wasn't a good idea
30th Sep 2018, 5:44 AM
Anna
Anna - avatar
+ 2
In Software World memory and security are very important factor and limited. So we have to maintain these two. Pointer helps to achieve this. Thanks
1st Oct 2018, 7:25 PM
Jai Verma
Jai Verma - avatar
27th Sep 2018, 11:29 AM
Yoshihiro Fujita
Yoshihiro Fujita - avatar
+ 1
In simple words -Pointer is used to point out the address of a variable
9th Jan 2019, 11:32 AM
Jkilop
- 1
So pointers are like fast food, u know not to eat it every meal but time efficiency overides the standard fastfoodophobia that keeps people from going overboard. Coo
11th Jan 2019, 11:01 PM
Z Ro DMG
Z Ro DMG - avatar