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.

Recommended Answers

All 4 Replies

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 :

#include<iostream.h>
void change(int a)
{
	a+=1;
}
void changeptr(int * a)
{
	*a+=1;
}
void main()
{
int a=0;
int * ptr=&a;
change(a);
cout<<"a="<<a<<endl;//see how the value didn't change
changeptr(ptr);
cout<<"a="<<a<<endl;//see how the value  changed becuase ew are dealing with a refrence >
}

output:
a=0
a=1

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.

1.
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) {
	a+=1;
}
void changeref(int& a) { //notice & instead of *
	a+=1; //notice no leading *
}
int main() { //notice standard is int return for main()
int a=0;
change(a);
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
}

2.
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);
  if(ID == CYLINDER_ID)
    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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.