+ 1
Trying to understand pointers in c++
I don't really know why we need the pointer. I understand the & sign stores directly the actual address of the variable. However, what I don't get is why we need pointers. Could I not just declare another variable and use that to store the other variable. For example. int x; int y = 50; int x = y; As opposed to int *x; int y = 50; *x = y; If I printed both values of "x" to the screen, they would just both give me 50. Why even use the pointer at all?
2 Respostas
+ 4
They are often used in apps that require speed efficiency because it's usually faster to pass a pointer as a parameter than to pass by value. For example if you have to pass an object that has a lot of data it takes a lot more time to copy it than to just pass its memory address. That's often the case in game programming where you have a lot of objects.
+ 1
Pointers weren't made only to hold a variable's address but there are many more uses of pointers.... mostly in linked lists , stacks and ques that aren't available in the sololearn's c++ course.....
The course is much basic and c++ can be extended to a wide range above it