Example on something that can only be achieved with pointers?

closed account (ETAkoG1T)
I started learning c++ not too long ago and even though I understand some of pointers I don't understand what they can do that you can't do without pointers! Is it just because it is more efficient? Writing an example of a program only possible with pointers would be nice, or just explain a program is also fine.

Thanks in advance!
Pointers are variables that hold an unsigned integer, representing a memory address.
They're different from "normal" int because math applies to them differently.
Look up "pointer arithmetic".

Is it just because it is more efficient?

For objects, it's usually more efficient and faster to pass a pointer (or a reference) than to copy the object.
(Pointers to any type have a predetermined, fixed size. Usually 32 or 64-bit.)

Writing an example of a program only possible with pointers would be nice, or just explain a program is also fine.

Pointers can also be used for polymorphism. Please go read the documentation.
http://cplusplus.com/doc/tutorial/
http://www.cplusplus.com/forum/beginner/60951/

Without pointers, you would be unable to make very big objects or very big arrays, unable to access hardware, and a whole bunch of other things in that thread.

Here's something very simple you'd find awfully hard to do without pointers (probably - I suppose you could have some mutant OS with the world's biggest stack, but you probably have a standard consumer OS like most people); make an array of 10000000 int.

int* p = new int[10000000];
Last edited on
closed account (ETAkoG1T)
Ty, sounds interesting! It would be nice if you posted a code example on polymorphism that I can check out and study a bit, thank you!
The tutorial Catfish2 pointed you at includes illustrative code.

See the tutorial!!

http://cplusplus.com/doc/tutorial/polymorphism/

Andy
Last edited on
Topic archived. No new replies allowed.