You are here:

C++/call by refrence

Advertisement


Question
Hi
i have a question about calling variables in functions

could you plz tell me what's the diffrence between:

power(&a)     and     power(*a)

i know that in power(&a) the address of 'a' will be change but will the value of 'a' change too?
or in power(*a) the value of a will change but will the address of it change too?

i mean the main values and addresses,not the values or addresses that we use in function power

Thanx
Bita


Answer
Dear Bita:

I am not sure what you mean.  

If you mean int power ( int & a ) and int power ( int * a ) then to call the first one you use something like power ( b ) where b is an int and you use power ( & b ) where b is an int or power ( c ) where c is a int * c or pointer to an int, in the second case.  

In the case of power ( b ), if the argument variable ( a ) is changed in power then b will be changed.  

In the case of power ( & b ) if the address the pointer is pointing to is changed then b is changed.  In other words changing *a changes b.  In the case of power ( c ) if *a is changed then *c is changed.  Changing the pointer in power ( or a ) has no affect on b at all.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.