+ 2
Why do we need pointer? What is the real use of it in real life? I do not get why we need to use pointer while we can just refer to the variable itself?
8 Respuestas
+ 1
Just think of it this way: passing an object that represents a complex class as a function parameter will create a copy of that class in memory (time consuming and memory inefficient), passing a reference to that class actually passes a pointer to the memory occupied by that object (time and memory efficient). If I'm wrong, please tell me.
+ 1
@Andrei Timofte
Yes you are absolutely right
But if the person is asking for whether he/she should use pointers or variables then his/her basic knowledge is not clear about pointers and variables. That is he/she should know about Sataic and Dynamic memory allocation which will eventually lead to the answer he/she seeks :)
@Andrei Timofte no hard feelings✌ .... your example is great and i liked it. I wonder why i didn't explained it that way😃
+ 1
Think about pointers as hyperlinks. What are the benefits of the hyperlink? If you acces a hyperlink, you will get the latest version of a Web page, even if it has changed. The same with pointers. When you pass argument to a function that function takes copy of the variables you use. For example:
void add(int a) {
a+=3;
}
int main() {
int b=1;
add(b);
cout<<b;
}
This program will show 1 because the function make a copy of b and add 3 to that copy. But if you use pointers, even if your variable changes, you'll get the latest values, just like a hyperlink. That's the main difference. I hope I helped you! :)
+ 1
We use pointers to hold addresses because it is some times impossible to impliment the expression for example swapping of numbers using functions.. So we use pointers for perform on addresses
0
First you need to understand two things:
Static memory allocation and Dynamic memory allocation
Well i cant explain everything here . Just look up Google for Static and Dynamic memory allocation difference ... you'll know what is the need of a pointer and when will you use a variable
Also Variables are for Arrays
Pointers are for Linked Lists
( ^ This is the basic difference ^ )
Look up the difference .. all the best :)
0
so as an analogy, a pointer would like creating a shortcut on your desktop for your music collection instead of copying the entire folder to the desktop?
0
Andrei Cirpici.....I got your explanation but I didn't understand that program. I think it will display 4 as the value of "b" changes. I've been told that pointers are like shortcuts but they got nothing to do with latest values. Please correct me if I'm wrong.
0
another reason is that it's just harder to identify a pointer in Cheat engine and this makes the game/app more secure