Hi, im just a beginner in C++ and was wondering what exactly are pointers used for? Are they just to refer back to something or are they used differently than that? I was reading through the chapter on pointers in my programming book and it doesn't really clarify.

Pointers are used for exactly what you say: referring to something (data) by its location rather than by the name you choose to call it. It's a subtle difference but an important one.

There are lots of ways to use this one simple principle to accomplish lots of powerful things in C/C++. So while it may not seem like a lot, it's pretty neat. If you want specific examples of cool things pointers allow you to do, say so and I'm sure many will oblige.

Alright, yes does anyone have any helpful examples or instances where pointers are used?

pointer are used commonly for 2 reasons and of course it has more:
1-pointers are used as a 'reference':
which that mean that if we had a function and this function changes the value of a variable and we passed this variable without using pointer this will be dealing with a photo(copy) of this variable but if we used pointer we can deal with the variable in memory it self look for this example :

void change(int a)
void changeptr(int * a)
void main()
int a=0;
int * ptr=&a;
cout<<"a="<<a<<endl;//see how the value didn't change
cout<<"a="<<a<<endl;//see how the value  changed becuase ew are dealing with a refrence >


2-for dynamic allocation in the memory:
that we can allocate a variable size for array using pointers with(new & delete):

and of course we use pointer to deal with C-style strings.

For C++, I would disagree with case 1 above, in this case you want to use a reference as in:

#include <iostream> //notice no .h here
void change(int a) {
void changeref(int& a) { //notice & instead of *
	a+=1; //notice no leading *
int main() { //notice standard is int return for main()
int a=0;
cout<<"a="<<a<<endl;//see how the value didn't change
changeref(a); //notice "a" directly
cout<<"a="<<a<<endl;//see how the value  changed because we are dealing with a reference

I agree that probably the main purpose for pointers is for dynamic allocation of memory (new & delete). However, in modern C++, especially with the new extension TR1, a much safer practice is to use smart pointers (especially the duo shared_ptr and weak_ptr).

So most of the time, references are better suited, and their main difference to pointers is that they cannot be "re-seated" in the sense that once a reference refers to some variable it can never be made to refer to something else, and they cannot refer to nothing (this is what makes them safer, but also makes (smart) pointers the only candidate for dynamic memory allocation). But now, considering smart pointers, many people now call pointers like "char*" or "int*" or whatever as C-style pointers because their role has been pushed to the realm C legacy code.

One useful purpose of (smart) pointers is to modify a unique object somewhere, like in case 1 above, but where it is more complex and thus the non-re-seatability of references become inconvenient. Also, pointers and references play a great role in static and dynamic polymorphism. One "cool" use I would say is this:

IVisibleObject* ReadVisibleObjectFromFile(const std::string& filename) {
  std::ifstream file(filename);
  int ID;
  file >> ID;
  if(ID == BOX_ID)
    return new CBox(file);
    return new CCylinder(file);

NOTE on the above: It is really not nice to implement this with C-style pointers like that, it should be with a boost::shared_ptr<IVisibleObject> (because the deallocator is packaged with the pointer), but this was for sake of example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.