declaring pointers vs regular variables

Aug 9, 2013 at 12:41am
I really do not see the difference between these two declarations:

int myvariable;
int * mypointer;

It is said that when you define a pointer, instead of containing actual data, it contains a pointer to the memory location where information can be found.

But doesn't the other variable declaration do the same? It obviously doesn't have data either. And it must be stored in a memory location as well. So I do not see the difference.
Aug 9, 2013 at 12:49am
int myvariable; creates an object of type "int", name "myvariable", and indeterminate value

int* mypointer; creates an object of type "int*", name "mypointer", and indeterminate value.

they are simply two different objects of different types. If you need to store a number, it can be stored in myvariable (myvariable = 7;) and not in mypointer. You can write std::cout << myvariable*10; to multiply and print it out as a decimal number.

If you need to store the address of some object of type int, it can be stored in mypointer, and not in myvariable. You can't multiply it, and if you print it, you will see a hexadecimal number. There really is almost nothing in common.
Aug 9, 2013 at 1:05am
So then in order to ever get a value from a pointer, it requires a few steps:

int myvariable;
int *mypointer;
myvariable = 7;
mypointer = &myvariable;
*mypointer // 7


That seems like a let of steps to get a value when you can just do this:

int myvariable;
myvariable = 7; // 7

So what's the practical use of a pointer?
Aug 9, 2013 at 1:30am
You'll know when you need them.
For example, operator new can only return a pointer. Or, for another example, an object may need to keep track of another object that exists elsewhere or can be replaced at any time.
Aug 9, 2013 at 8:33am
You will see the difference when you start lerning data structures like Stack, Linked List, Queue, Binary Tree, etc....
Aug 9, 2013 at 10:54am
See my post in this thread:

http://cplusplus.com/forum/general/105593/

for some reasons to use pointers.

(I'm starting to think I should write this up as a short article, since I seem to be linking to it a lot)
Aug 9, 2013 at 2:49pm
In general, do not use a pointer unless you know that you can't do without one. And as Cubbi points out, you'll know when you need one.

A pointer is one way of indirectly referring to an object, and the idea of referring to another object is immensely useful in practice. For instance, I share my mother with my siblings, we all refer to the same mother; in programming terms, each of us can't define a separate mother - all of us refer to or 'point' to the same mother. For a person whose mother is no more, the pointer would be a null pointer; a pointer that does not point to an object.

The most common use of a pointer (or something that has pointer semantics) is when we want to iterate through a sequence. For example, if we have a list of books (we do not know how many books there are at the time we write the code), and we want to print out the title and author of each of these books, our code would look something like:

1
2
3
4
5
6
initialize a pointer to point to the first book.
repeat:
      print out the title and author of the book pointed to
      make the pointer point to the next book
      if the pointer is not null (there is a next book), go to repeat
      else we are done.




Last edited on Aug 9, 2013 at 2:49pm
Topic archived. No new replies allowed.