Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm a cs student working towards my degree. My road to graduation is still a pretty far deal. However, i want to make sure that im doing everything i can to surpass all the others cs students.

Anyways, i'm trying to wrap my head around why we use pointers or why ever bother using them in my code. I understand that a pointer points to an address. That address demonstrates the variable memory location.

So in essence, i can determine what address my variable have. However, assuming every user is using a computer, wouldn't the address change for that specific variable, since its a dynamic value (assigned by hardware etc.)?

The only time i feel i will need it is in a circumstance were i want to pass specific objects as parameters, but the only work around is through pointers (not there yet, thats why i can say what it would be).

Please if anyone wants to elaborate, please do. I want to understand memory management, and how it applies in our applications.

Thank You,
Jonathan V.

What I have tried:

C++
unsigned short shortVar = 5;
std::cout << "shortVar: " << shortVar << std::endl;
std::cout << "Address of shortVar is: " << &shortVar << std::endl;

//OUTPUT
/*
shortVar: 5
Address of shortVar is: 0x7fff51986b2a
*/
Posted
Updated 23-Mar-17 23:34pm
Comments
PIEBALDconsult 24-Mar-17 0:26am    
Because.
RAMASWAMY EKAMBARAM 24-Mar-17 1:25am    
"C wears well as one's experience with it grows" - Preface to "The C Programming Language - K & R".
Pointers wear exceptionally well as one's experience increases! Callbacks & Event Handlers, thread execution, polymorphism (C++) all depend on function pointers.
Pointers are probably the main reason that C continues to hold its own!

In C, passing an address is the only way to pass a parameter by reference to a function so that you can modify the variable directly. Contrast to passing by value, you are only modifying a copy of the variable, the original variable remains unchanged.

And for a big structure, passing by address is faster.
 
Share this answer
 
Because C/C++ are designed to expose the pointers to programmer that are responsible of their handling.
Other languages are using pointers too but it is hidden to programmers.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Addresses of items in a computer program are relative to the start of the relevant section of the program. This could be the CODE section, where the instructions are stored, or the DATA section, where most of the data is. The hardware translates these relative addresses to absolute after the program is loaded. A basic book on computer architecture will explain the details for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900