>
> It displays 10. *C++ passes the address of *v in main to afunction by
> value. *The parameter, x, in afunction is the value of the pointer, and
> changing it doesn't change v in main.
>
> I think it'll become clearer if you try adding some output to see the
> changing values, maybe like so:
>
> void a(int *x) {
> * * std::cout << "a,x " << x << " a,*x " << *x << std::endl;
> * * x = new int(12); // leaks memory
> * * std::cout << "a,x " << x << " a,*x " << *x << std::endl;
>
> }
>
> void testa() {
> * * int v = 10;
> * * std::cout << "testa,&v " << &v *<< " testa,v *" << v << std::endl;
> * * a(&v);
> * * std::cout << "testa,&v " << &v *<< " testa,v *" << v << std::endl;
>
> }
>
> int main() {
> * * testa();
>
>
Thanks for the post, I should know better. If x had not been
reassigned then it would display 12. So I think it would be correct to
say that V was passed by reference (by its address) but it's address
was passed by value.
I had put it many display statements and they were telling me what you
told me but not as concisely and I did not see it.
Thanks again,
jvh (AKA an average programmer)
|