The code passes an argument by address to ptr_copy fx. Problem is I want to pass by value so that arguments are copied to a parameter and do not change the original argument. The copy output should be 12 99 12 but I am getting 12 99 99 b/c the argument is being changed from within the fx. How do I pass value so output is 12 99 12? The first number is from main; the second within the fx; the third from main again. So if its copied by value main should stay the same.
#include<iostream>
//changes argument from inside the fx
void ptr_copy(int * n_ptr)
{
std::cout<<"fx copy - see if it copies or replaces"<<std::endl;
*n_ptr = 99;
std::cout<<*n_ptr<<std::endl;
}
int main()
{
int val_x = 12;
int * n_ptr;
n_ptr = &val_x;
std::cout<<*n_ptr;
std::cout<<std::endl;
ptr_copy(&val_x);
std::cout<<*n_ptr;
return 0;
}
But the task at hand is to pass a pointer to a function by address (passed by value). Change parameter, change copy. Original pointer argument will not be changed.
pass a pointer to a function by address (passed by value)
OK... so the pointer is already being passed into the function by value - because, as we know, that's the default way everything gets passed in C++.
Change parameter, change copy. Original pointer argument will not be changed.
It seems to me that what it's saying is that you create an int that's a copy of the value being pointed to by the argument, and change the value of that int.
Best I can do so far, implemented in code. Still getting a reference result instead of copy. Result 5 (before fx) 8 (in fx) and 8(in main). This should be 5 not eight.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
void foo(int * ptr)
{
std::cout<<*ptr;
*ptr = 8;
}
int main()
{
int x_val = 5;
int * n_ptr = &x_val;
foo(n_ptr);
std::cout<<*n_ptr;
return 0;
}
The pointer that you're passing into foo() contains the address of the x_val variable in main(). So when you change the value of the memory ptr is pointing to, you're changing the value of x_val.
I thought an extension of the pointer being equal to the value it is pointing to, was considered part of the pointer. My mistake.
Ok, so in my original post I was referring to value(s) but these were really addresses, not the value to which the ptr was being pointed to. values-->addresses
I'm not sure what you're doing with lines 10 - 12. At line 6, you create a copy of the value being pointed to by ptr, and at line 9 you're changing the value of the copy.
Why then create another int var, and another pointer L?
Note that you never initialise var, so the value is undefined. The value being pointed to by L is therefore, obviously undefined.
Everywhere you seem to be printing out the value of the pointer - i.e. an address, whereas in your original code, you were printing out the value being pointed to - i.e. an int. Which data are you really interested in here?
I do agree, too many variables. Having more does not make you less lost.
With less:
1 2 3 4 5 6 7 8 9 10 11 12 13
void foo( int * ptr ) {
using std::cout;
cout << "ptr has " << ptr << '\n'; // address
if ( ptr ) {
cout << "A. value " << *ptr << " at " << ptr << '\n';
int val = *ptr; // create copy of value
cout << "B. value " << val << " at " << &val << '\n';
ptr = &val; // make ptr point to different object
cout << "C. value " << *ptr << " at " << ptr << '\n';
*ptr = 8; // change pointed to object
cout << "D. value " << val << " at " << &val << '\n';
}
}
I cleaned up the code. I now have one program each below demonstrating pass by value of a ptr (address) and one passing argument by address. In the first the original argument is changed from within the fx. In the second a copy is made of the parameters, it is modified, but does not change outside the function. So far the only data i'm interested in here are ptr values -- now I have a better idea of what they are , and the program is working as expected. Thx
//n_ptr argument changed from within fx
//note ampersand on bottom version
#include<iostream>
//n_ptr argument changed from within fx
void foo( int * &ptr ) {
if ( ptr ) {
std::cout<<"copy to ptr parameter: "<<ptr<<std::endl;
int * new_ptr; //address changed -- effects original argument
ptr = new_ptr;
std::cout<<std::endl;
std::cout<<"new ptr address generated in fx: "<<ptr<<std::endl;
}
}
int main()
{
int x_val = 5;
int * n_ptr = &x_val;
std::cout<<"Main n_ptr: "<<n_ptr<<std::endl;
foo(n_ptr);
std::cout<<"Main-->ptr fx - changed from within function: "<<n_ptr<<std::endl;
return 0;
}