Title: Why have pointer parameters?
Tags: c++;pointers;parameters
Question: 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.
Comment: @greyfade: When you create a function with pointer arguments. Why would you want to do this?

Comment: @Matt Greer: I know what passing by reference, value, and pointer is. I know how to do it; I just don't know why would I ever want to pass by pointer vs. reference. I know why I would want to pass by value vs reference but not by pointer.

Comment: It's entirely possible the OP has never heard of references and from his perspective it's the debate between "regular" parameters and parameters that are pointers.

Comment: For clarification: Are you talking about pointers as parameters to functions?

Comment: As opposed to _what_?  Do you question the use of any indirect reference or just pointers vs references?

Comment: Possible duplicate: http://stackoverflow.com/questions/162941/why-use-pointers

Here is the accepted answer: In Passing a modifiable parameter to c++ function 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:
It can be null
It can be changed (to point to something else)
It must be deleted
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.

Here is another answer: 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.

Here is another answer: 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).

Here is another answer: 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;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << some_function(num1) << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl; //prints 16
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << num1 << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl; //prints 15
Comment for this answer: Yes but I can just pass by reference instead. Why would I want to have pointers?

Here is another answer: 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.

Here is another answer: Compatibility with C code where references aren't available.
Variable sized arrays.
Option to have a missing parameter, i.e. NULL.
Comment for this answer: Variable sized arrays aren't in C++. (Or were you referring to dynamically allocated arrays?)
Comment for this answer: Ah.. thanks for explaining. +1
Comment for this answer: @Billy Oneal, It's variable to the function, not to the caller. I.e. `void Foo(int* array, int count); int first[10]; int second[20]; Foo(first, 10); Foo(second, 20);` You could declare instead `void Foo(int array[], int count);` but it would be exactly the same thing, just a pointer in disguise.

Here is another answer: 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).
Comment for this answer: Note that polymorphism works correctly with references as well.
Comment for this answer: Your first reason doesn't require pointers.
Comment for this answer: Yeah, I said "and references". C++ happens to have two ways of invoking polymorphism. Pointers being one of them.

Here is another answer: 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.

Here is another answer: 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.
Comment for this answer: @iconiK point 1 can be accomplished with references, but it's definitely not moot because of the reason you stated.  Objects are frequently much bigger than 10 bytes. Evaluating and passing the address of an object can be a huge speed up over copying the object, especially when it's a frequently called function.
Comment for this answer: Point 2 can also be accomplished with references. Point 1 is totally moot today - moving a few bytes is extremely fast, getting speed-ups is better done in more efficient algorithms and protocols, not shaving 10 bytes off a function call.
Comment for this answer: @Graphics Noob, which is why pimpl and implicit sharing are excellent ways to speed up copying.
Comment for this answer: Point 2 is a question of syntactic preference: some like to make it explicit that they're passing an address, not a value.  Point 1 is not "moot".  That would mean it doesn't matter how big your data structure is.  Try passing a 10 MB linked list by value some time.

Here is another answer: Plenty of reasons, but references should be the "standard". Use pointers only if a reference won't do. Some pros and cons:
Pointers can have NULL values, something that's extremely useful
Pointers can be of generic types and be reinterpreted as other types (though don't you dare use void *)
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.

Here is another answer: There is one more difference between pointer and references:
-pointer can be a NULL-pointer
-reference references always an real object [f(T &amp;v); f(NULL); isn't really good idea]

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 11, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:06/17/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><pointers><parameters>', 'Title': 'Why have pointer parameters?', 'ViewCount': 749, 'id': 3064976, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}