Pointers

Feb 3, 2011 at 9:07am
I have managed to get to grips with pointers and think I understand waht is going on here.

The only thing I can't get my head round is why I would use a pointer to get the value of a variable from it's storage location when I could just use the variable name. ie what is the advantage of referring to the value with a pointer rather than the variable name, surely the end result is the same in either case in that the value is the same.
Feb 3, 2011 at 9:27am
Have you read about functions? And about pass-by-value & pass-by-reference? if not, read it and you will get to know about atleast one reason that why we need pointers. :)

There are many other reasons that other members of the community may list down for you or you may come across as you progress in your C++ course.
Feb 3, 2011 at 10:51am
Read up on pointer arithmetic and dynamic memory allocation.
Last edited on Feb 3, 2011 at 10:52am
Feb 3, 2011 at 12:11pm
closed account (zb0S216C)
Here's a good article: http://stackoverflow.com/questions/162941/why-use-pointers
I have an account with Stack Overflow and I trust the information harvested from there.
Feb 3, 2011 at 12:31pm
closed account (z05DSL3A)
The only thing I can't get my head round is why I would use a pointer to get...

This may seem like a flippant answer but here goes; Don't use pointers unless you have to, when you have to use pointers you will then now why you use them. As long as you know how to use them the why will come later.
Feb 3, 2011 at 12:34pm
ive always thought because its quicker and cheaper to pass a pointer to the data then moving and passing around the actual data itself. but i could very well be wrong im just a beginner.

The amount of data could be huge, so it just seems natural to use a pointer to it instead.
Feb 3, 2011 at 12:42pm
ive always thought because its quicker and cheaper to pass a pointer to the data then moving and passing around the actual data itself. .


Consider a reference rather than a pointer. The general advice is use references where you can and pointers where you have to.
Last edited on Feb 3, 2011 at 12:43pm
Feb 3, 2011 at 12:47pm
ok Moschops I will remember that.
Topic archived. No new replies allowed.