Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; If I understand this all correctly, pointers are like a shortcut on the desktop. I understand they point to something.
...

Pointers

If I understand this all correctly, pointers are like a shortcut on the desktop. I understand they point to something.

However, why are they more useful ? Do they take up less memory ? Please don't say they're quicker, cuz it's not true, they take up an extra line to declare than normal variables. Why are they better ?

And is there anything I should know about them that's very important ?

Pointers and regular variables are VERY similar, you can make one look like another via derefrencing. Eg...

int areg = 10;
int *ptr = &areg;
int breg = *ptr;

Pointers ARE faster. Instead of passing an entire-- say, 32b structure to a function, why not pass a 4b pointer? It's much faster.

If you don't understand them, or see a need for them right now-- don't use them. For me, pointers were the hardest concept to understand... but today I couldn't code without them. Eventually pointers will work their way into your toolbox.

Here we made use of the fact that arrays store their contents in increasing addresses. Thus the length was found by subtracting the higher address from the base address.

Also, you can see that setting a pointer to NULL can signify that something was not found, an often used trick.

Pointers are faster to pass as parameters to functions than large stuctures. When you pass a large structure to a function, a replica is made, which takes time and memory. Had you passed a pointer to it, the function would've used that 4 byte address and and thus wouldn't have replicated the data.

Honestly, there are hundreds of uses for pointers, and I'm sure you'll realize that soon enough!

Simpliest...
1 byte contains 8 bits.
So in 32 bits environment, there must be 4 bytes per one memory address.
Pointer contains memory address, so one pointer consumes 4 bytes.
You can compare shotcut with pointer but thet are not the same thing. Shotcut contains URL, but pointer contains memory address (at low level). And pointer can do much much much more, if you learn.