Trying to understand pointers in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

10th Dec 2016, 2:36 AM
Eddy
2 Answers
+ 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.
10th Dec 2016, 10:24 AM
Karl T.
Karl T. - avatar
+ 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
10th Dec 2016, 5:15 AM
Jatin
Jatin - avatar