address passed by value

Apr 18, 2016 at 4:04pm
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.

(sorry for the long post)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}

Last edited on Apr 18, 2016 at 4:07pm
Apr 18, 2016 at 4:14pm
Surely you know how to pass an integer by value? It's the default way arguments are passed into a function in C and C++:

1
2
3
4
void int_copy(int n)
{
  // ...
}
Last edited on Apr 18, 2016 at 4:14pm
Apr 18, 2016 at 5:03pm
Thx for the reply: In response -- of course!

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.

So how do I implement your suggestion here?
Apr 18, 2016 at 5:50pm
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.
Apr 18, 2016 at 6:43pm
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;
}
Last edited on Apr 18, 2016 at 7:01pm
Apr 18, 2016 at 6:56pm
I suggested:

you create an int that's a copy of the value being pointed to by the argument, and change the value of that int.


Your function doesn't seem to do that.

I hope you can see why what you've done is giving you the result you're getting?
Apr 18, 2016 at 7:05pm
I hope you can see why what you've done is giving you the result you're getting?


I am having difficulty with that. I'll keep at it though.
Apr 18, 2016 at 7:12pm
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.
Last edited on Apr 18, 2016 at 7:12pm
Apr 18, 2016 at 7:14pm
You create an int that's a copy of the value being pointed to by the argument, and change the value of that int.


Do you mean within the function?
Apr 18, 2016 at 7:25pm
In your example the ptr is a copy of n_ptr. Both pointers hold the same value: address of x_val.

If you do change the ptr, the n_ptr will not change, because ptr is a copy. Your code does not change ptr at any point.

If you do dereference the pointer ptr (and that you verily do), then you do get access to x_val and therefore your line 8 indeed changes x_val.

It sounds like you want to do:
1
2
3
4
5
6
7
void foo( int * ptr ) {
  if ( ptr ) {
    int val = *ptr;
    std::cout << val;
    val = 8;
  }
}
Apr 18, 2016 at 7:36pm
Your code does not change ptr at any point.


1
2
3
4
5
void foo(int * ptr)
{
	 std::cout<<*ptr;
	*ptr = 8;
}



setting *ptr val to 8?
Apr 18, 2016 at 7:53pm
No. That's not changing the value of ptr. It's changing the value of the memory at the address stored in ptr.

I suspect you haven't actually understood what a pointer is. A pointer is a variable that stores an integer, and that integer is a memory address.
Apr 18, 2016 at 8:46pm
That's not changing the value of ptr.


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

void foo( int * ptr ) {
  if ( ptr ) {
	 std::cout<<"fx copy: "<<ptr<<std::endl;
    int val = *ptr;
    //std::cout << val;
    std::cout<<std::endl;
    val = 8;
    int var;
    int * L = &var;
    ptr = L;
    std::cout<<"fx copy with address ptr change: "<<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 returned - unchanged: "<<n_ptr<<std::endl;


	return 0;
}


Last edited on Apr 18, 2016 at 9:25pm
Apr 19, 2016 at 12:08am
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?
Apr 19, 2016 at 9:41am
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';
  }
}
Apr 19, 2016 at 2:56pm

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;
}




Main n_ptr: 0x23fe0c
copy to ptr parameter: 0x23fe0c

new ptr address generated in fx: 0x484f80
Main-->ptr fx - changed from within function: 0x484f80


//copy of parameter: can be changed within fx but not original argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>

//copies n_ptr
//original address unchanged

void foo( int * ptr ) {
  if ( ptr ) {
	 std::cout<<"fx copy: "<<ptr<<std::endl;
     int * new_ptr; //address changed -- will not change original argument?
     ptr = new_ptr;
     std::cout<<std::endl;
    std::cout<<"fx copy with address ptr change: "<<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 returned - unchanged: "<<n_ptr<<std::endl;


	return 0;
}




Main n_ptr: 0x23fe14
fx copy: 0x23fe14

fx copy with address ptr change: 0x40115b
Main ptr fx returned - unchanged: 0x23fe14
Last edited on Apr 19, 2016 at 3:01pm
Topic archived. No new replies allowed.