Recently, I've been dabbling with Pointers in C++. According to what I've read, pointers variables are used to pass variables by reference (similar to ByRef in VB) or function pointers (delegates in C#). I'm pretty sure pointer usage is not confined to these examples. I was hoping to know some applications where pointers are necessary. Most tutorials on the net try to explain the pass by reference usage. Thanks in advance and sorry for the noobish question :)
-
1You might want to check http://www.cs.cf.ac.uk/Dave/C/node10.html and http://duramecho.com/ComputerInformation/WhyCPointers.html – praxmon Jul 23 '12 at 04:55
-
2There are two ways to pass a variable: pass by value and pass by reference. Pass by pointer is actually pass by value, not reference. This might help you http://stackoverflow.com/questions/4426474/is-passing-pointer-argument-pass-by-value-in-c – madu Jul 23 '12 at 04:56
-
1You're probably going to see varying answers for as long as this is tagged both C and C++. – chris Jul 23 '12 at 04:57
-
@PrakharMohan Seems useful thanks ! – Ahmed Mohamed Jul 23 '12 at 04:58
-
@madu So it is pass by val. but allows the function to alter the var?? – Ahmed Mohamed Jul 23 '12 at 04:59
-
2Dynamic arrays have equal validity in C and C++, and are a great use for pointers. In the latter, you're just better off using something like `std::vector` that implements the array for you, but the pointer is still there in the vector. – chris Jul 23 '12 at 05:00
-
1As a previous answer stated, function pointers are great for callbacks, but again, the latest version of C++ encapsulates this in a nice `std::function` with less confusing syntax and more flexibility. – chris Jul 23 '12 at 05:04
-
1@engheema Pass by pointer is actually pass by value, but the value that is passed is the address of the variable. So when you pass the value to a function that takes pointer as an argument you need to pass the address. So depending on the way you look at it, it behaves as pass by reference because the function can change the actual value, but that is because what you pass is actually the value of the address. Maybe somebody can correct me if I'm wrong. – madu Jul 23 '12 at 05:32
3 Answers
Generally you can use pointers only when you don't have any other option. For eg In C there is no way of passing a variable "by reference" to a function.
Also in C++ without pointers you can't really achieve run time polymorphism.
- 4,483
- 2
- 31
- 56
-
You mean I can use a base class pointer to point to a derived class object?? – Ahmed Mohamed Jul 23 '12 at 05:04
-
1
As a note, if you are programming in c++ you should start learning with smart pointers:
http://en.wikipedia.org/wiki/Smart_pointer
Smart pointers are equivalent to pointers but safer.
Here is a good tutorial and exercise for using c++ smart pointers:
http://www.ida.liu.se/~TDDD38/exercises/Smart_Pointer_II/Smart_pointer_II-11-en.pdf
- 30,083
- 25
- 91
- 165
-
You shouldn't be using smart pointers until you understand normal pointers. Smart pointers are fine for special cases, but plain pointers remain the most general. (In this case, the OP was talking about taking the address of a variable. Smart pointers will not work for that.) – James Kanze Jul 23 '12 at 08:02
There are a number of reasons you might want to use pointers. One of the most obvious is an optional argument:
void f(int a, int const* b = NULL);
With f, you can test whether b is null or not, and act in
consequence.
Another very frequent reason for using pointers is for navigation. You might want to look at the observer pattern, for example. But there are many other cases where you need to navigate between objects (with relationships which vary dynamically).
Finally, pointers are used when objects are allocated dynamically. This isn't nearly as often in C++ as in languages like C# or Java (which basically allocate everything, or almost everything dynamically). In C++, the usual reason for allocating something dynamically is that the object isn't copyable (it has identity) and has a lifetime that is independent of program scope. Another reason is for subobjects in an object with a dynamic topology: the nodes for graphs and such, for example. Polymorphism is also occasionally a reason; if the actual type of an object depends on some runtime condition, you have to allocate it dynamically.
- 146,674
- 16
- 175
- 326