Possible Duplicates:
Why use pointers?
Passing a modifiable parameter to c++ function
Why would I want to have pointer parameters? The only reason I can see is for small functions to attempt to reduce confusion and a smaller memory footprint.
Possible Duplicates:
Why use pointers?
Passing a modifiable parameter to c++ function
Why would I want to have pointer parameters? The only reason I can see is for small functions to attempt to reduce confusion and a smaller memory footprint.
There are many reasons to have pointers as parameters. A major reason is polymorphism. The pointer can point to a base class object or a subclass, and will call methods on that object accordingly.
I recommend Accelerated C++ by Koenig, he does a good job of explaining how polymorphism works in C++ via pointers (and references).
Pointers are the only way to accept C-style strings and arrays. They're also the only way to share data between multiple threads (besides global objects, which... ew).
Other than the standard uses for a pointer parameter (that others have already stated), one reason that I can think of is to implement callbacks. You can have a function that accepts a function pointer as a parameter.
I assume you mean as opposed to references? The thing that you can do with pointers is set them to NULL, which allows you to specify an uninitialized state. This can sometimes be useful, but the corollary to that is that if you want to enforce that NULL cannot be passed to your function then using a reference makes that clear.
Two reasons:
A pointer on a 32-bit platform is only 4 bytes. If the data you're passing to the function is more than 4 bytes, you save some call time by passing the structure by reference instead of by value. (You may give up this performance advantage in indirection costs later, but that's where profiling comes in.)
You can pass a non-const pointer to let the function modify the passed data.
if you pass a simple variable to a function, the function creates a copy of that variable for use within the function's scope. When the function completes execution, any changes made to the passed simple variable won't be shown. This is because the variable itself was never altered within the function only a copy of it was. Passing a pointer to the variable to a function solves this problem.
int some_function(int num) {
return num++;
}
int num1 = 15;
std::cout << some_function(num1) << std::endl; //prints 16
std::cout << num1 << std::endl; //prints 15
IMO, pointers are made out in a lot of C++ documentation to be far more important than they actually are, whereas references are rarely discussed in introductory material (other than brief mentions in terms of parameters).
While pointers certainly have value (arrays, string, dynamic memory, etc.), a lot of their functionality can be implemented more simply with references. The only time I really use pointer parameters regularly is when I am using a third-party library which has such a use built into it.
Using references removes the copy-assignment step implicit in straight class parameters while not requiring tiresome pointer dereferencing (using * or ->) over and over again. In addition, learning to use references is important for applications such as operator overloading, which is vital for precision control of your code.
In http://stackoverflow.com/questions/1322517/passing-a-modifiable-parameter-to-c-function/1322547#1322547 I answered when to use a reference instead of a pointer.
Conversely, prefer a pointer to a reference when any of the following are true:
Some people also prefer a pointer when the thing that's being pointed at should be mutated, saying that the difference between a const reference and a non-const reference isn't obvious enough to the reader.
Plenty of reasons, but references should be the "standard". Use pointers only if a reference won't do. Some pros and cons:
Pointers can be used to pass huge arrays without a huge memory footprint
In the case of generic types, compiler optimizations can operate better with references, because the compiler has more information on used types
References arguably make code more readable, more in-line with java and c# and thus better readability by other types of developers
References do not actually pass the entire object, but rather a pointer to it, however more type information is available to the compiler.
There is one more difference between pointer and references: -pointer can be a NULL-pointer -reference references always an real object [f(T &v); f(NULL); isn't really good idea]