Explain to a noob: pointers

Basically I've read that it points to the location in memory of another variable, ok, but how does this help me? what does it really mean? I just started learning C++ and I'd really want to understand what I'm doing xD
Thank you in advance!
Pointers are variables which store a memory address.

Read more here:
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/articles/ (browse around)
I have read the articles, the thing I donĀ“t really understand is why use a pointer to change the value of a variable when I can change it by calling it everytime.
It really got me troubled, I mean, from what I saw from those examples I understood that you can call a pointer to change the value of one or more variables, but you could also just call the variables and change the value. And also, what can you do with the memory location of a variable?
Thank you in advance!
why use a pointer to change the value of a variable when I can change it by calling it everytime.


int* p = new int;

So tell me, how are you going to change the value of this int without using the pointer?
Other examples of "change by pointer": think of passing variables to functions. If you do so "by value", the function works with a temporary copy of the variable and any change made is not reflected in the original variable. If you pass a pointer to a variable, you CAN change it inside a function.

Secondly: efficiency. Imagine having a group of large objects, and you need to find one with the highest value for a certain criterium. Doing it "by value":
1
2
3
4
Object best;
for (all n objects) {
    if (objA > best) best = objA; // Copies entire object
}

Which in the worst case would require n full copies of the entire Object. Instead, you can do:
1
2
3
4
Object* best;
for (all n objects) {
    if (objA > *best) best = &objA; // Copies address
}

Instead of copying an object (of any size), you copy an address of 4bytes (?). 4*n instead of 4*sizeof(Object) is a big change.


Another important reason for pointers is data structures. A linked list is the easiest example: if you don't know how large a list is (thus can't simply assign an array of N items), or the sequence can change often, you can organize data by pointers.
1
2
3
4
class ListItem {
    // Satellite data
    ListItem *next, *prev;
}

Rather than changing the sequence by swapping (= 3 copies!) Objects in an array, you adjust the sequence by changing the next and prev pointers.
Last edited on
from what I saw from those examples I understood that you can call a pointer to change the value of one or more variables, but you could also just call the variables and change the value.

Not all memory locations have names. Som memory you aquire at runtime, so you must handle it using a pointer.

C only has (function) call by value. So to implement call by reference, a pointer to the variable is passed:
1
2
3
4
void init(int* n)
{
    *n = 2;
}


So memory mapped hardware map into specific addresses. How could you access that memory? By pointing to it.

The examples are almost endless.
Topic archived. No new replies allowed.