Pointers

I read the article here about pointers, but what I didn't get was: What is the point of using pointers?
And since I couldn't use it, I didn't understand it properly
Can some one please explain it to me?
Reasons for using pointers include:

Better memory usage vs. making copies (although references also have this benefit).
The ability to easily "link" certain classes together (such as in a linked list).
Polymorphism, which is extremely useful when you get to inheritance.
Easy memory access, if you should want it.

Does that answer your question?

-Albatross
Last edited on
Easy memory access, if you should want it.

How does it do so? I didn't understand much about them.

And what is polymorphism?
Here's a very simple example of one possible use for a pointer. Imagine you are writing code for some hardware, and the manufacturer's manual explains that the state of the push-button that a user can push on this hardware will always be written to memory location 0x343421FF

You need some way to read the value at that memory location.

1
2
3
4
5
6
7
8
9
10
int* pushButtonState; // Create the pointer
pushButtonState = 0x343421FF; // Pointer now directed to the appropriate memory location

if (*pushButtonState == 1) // Read the memory
{
  cout << "Button is currently being pushed.";}
else
{
  cout << "Button is not being pushed.";
}


If you can read a specified memory location without using a pointer, I'll be impressed :)
One way I could describe a pointer is by saying that it's a variable that stores a memory address. You're free to manipulate this memory address however you want, although it won't always refer to a location that you can write to or even read from unless you know exactly what you're doing. That's what I meant by saying easy memory access.

EDIT: Dangit Moschops!

Polymorphism is a property of objects in C++ that allows you some flexibility in choosing which what type of class your pointers point to. I can't really explain the details to you unless you know about classes and inheritance.

-Albatross
Last edited on
Understood. Thanks a lot.
O.K. I will go to those parts soon.
Last edited on
Topic archived. No new replies allowed.