0

Why I need pointer?

int x = 5 - - - - - variable with value int &x - - - - adreess Why I need *p?

6th Mar 2021, 3:59 PM
Name
Name - avatar
7 Respostas
+ 1
Name You don't need to take it into another location. You just need to change the values at the locations. That's why pointers are so useful. Suppose you have a function that takes a large amount of data and just makes a small change in the data. If you had only pass by value, the whole data needed to be copied into the function, since that's what pass by value does. This would take an enourmous amount of memory and time. On the other hand, if you use pointers, all you need to do is pass the memory address of the variable using '&' symbol and in the function, you can change the value at the location using '*' symbol.
7th Mar 2021, 2:08 AM
Soumik
Soumik - avatar
+ 1
pointers are faster than referencing arrays by index
6th Mar 2021, 4:10 PM
Slick
Slick - avatar
+ 1
Suppose you have bucket A containing milk and bucket B containing water. You need to swap the contents of the buckets without using a third bucket. How would you do it? You can do something not so common - just change the name of 1st bucket to B and the name of 2nd bucket to A. This is very similar to how pointers work. The buckets are variables and their contents are values and when you change the names of buckets, it's like changing the value at a memory address
6th Mar 2021, 5:33 PM
Soumik
Soumik - avatar
+ 1
Soumik, when I create a variable it has a location... then why I need to take it to another location?
7th Mar 2021, 1:48 AM
Name
Name - avatar
0
didn't get it 😅slick
6th Mar 2021, 4:28 PM
Name
Name - avatar
0
https://code.sololearn.com/cNEltVu3tDG8/?ref=app pointers are set to a memory address the pointer in the code i posted is set to the memory address of the variable x
6th Mar 2021, 4:37 PM
Slick
Slick - avatar