Pointers

Started by
2 comments, last by Hot Dog 17 years, 10 months ago
What are pointers used for in C++?
Advertisement
clicky
-loop iteration
-dynamic memory allocation
-function pointers
-dividing a datam into smaller parts witbout moving its bits around
Sorry... don't quite understand your question.

Pointers in C++ are alot like pointers in other languages. Pointers *point* (no pun intended) to a location in memory. It is like a handle (my own term, not the C++/CLI handle) that allows you to manipulate that piece of memory.

Some common uses of pointers:

1) manipulating values/objects
Instead of passing around by value that can get pretty inefficient in terms of the value/object copying, you pass around the pointer to that value/object. You can then manipulate that value/object via its pointer using "->" operator.

2) manipulating functions
Using function pointers (i.e.: pointers that point to functions of specific prototype), you can actually get your program to call functions during runtime, whose specific names are not known at compile time. A related use is also to implement function callbacks (i.e.: basically a mechanism by which a function can "callback" the function that called it in the 1st place).

3) manipulating arrays
This is actually similiar to point (1) above. Instead of passing around and manipulating the actual array elements, you pass around and manipulate them via the array pointer.

There are other great uses for pointers.

Hope that was useful to you.

Have fun! :)

Joshua

This topic is closed to new replies.

Advertisement