C++ Pointers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ Pointers

First of all. I have lost to many braincells for this 😵 So... int var = 50; int *p; p = &var; This "*" is to learn for content when we cout. //*p = var when p = &var This "&" is to learn the location in memory (address). Q: Why we need this information ? (0x29fee8 (var's memory location)). Thanks.

9th Nov 2021, 6:36 AM
Chris Papadopoulos
Chris Papadopoulos - avatar
2 Answers
+ 4
What if you request some memory from your system and it only provides you with the starting address of the memory block it allocated for you ? ( You would need some way to store that location so that you can use it in your program ) What if you have a massive object ( a custom data structure ) that you want to move around the program. Wouldn't it be better to just send the memory address of the object instead of sending a copy of the object itself ? ( obviously there are times where you might NEED a copy itself but we are talking about the case where you don't ) There are a ton of places where storing a memory adress and knowing what kind of data is stored there can be usefull. I would recommend continuing your C++ course to discover some yourself.
9th Nov 2021, 7:59 AM
Arsenic
Arsenic - avatar
+ 3
We rarely need to know the address because this information is useless after the program ends. The variables are deallocated and therefore whether the addresses belong to the variables is unknown. The number of circumstances where you need to know your address while running the program is few. But even though we don't need to know it most likely, we still use it. The concept and use of pointers are rather morenimportant than the address itself.
9th Nov 2021, 8:23 AM
你知道規則,我也是
你知道規則,我也是 - avatar