Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing by address vs pointer 2

Status
Not open for further replies.

Warrioruw

Programmer
Jan 30, 2004
24
0
0
CA
Hi all,

I am a bit of confused with parameter passing. What is the difference bettwen passing by address and by pointer? For example, you have a function:

void Addition(int& a, int& b)

vs

void Addition(int* a, int* b)


Thanks.

 
>void Addition(int& a, int& b)

Addition can only modify the contents of a and b, it cannot destroy them.

>void Addition(int* a, int* b)
Addition can destroy the parameters by a simple
Code:
delete a;
delete b;

The caller is giving Addition the full power to do what it want with the parameters.

If Addition is not supposed to destroy them (and the name Addition suggest its not) there is no need in even make it possible.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Also, the first one, passing by reference, ensures that a value is actually passed in. The second one, passing by pointer, allows the user to pass in a null pointer.

So if you Addition is not supposed to destroy the parameters and it does not expect a null pointer, then there is no need to pass by pointer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top