value of reference does not change?

I know how passing by ref and value works well I thought I did haha

but I'm still confused as to why this line of code does not change the value in number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>

using namespace std;

int functionA(int *x){


  int y = 55;
  x = &y;
  return *x;
}

int main()
{


   int number = 60;
   cout << &number << number << endl;
   functionA(&number);
   cout << number;

}


and why this code does actually DOES change the value of x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


#include <iostream>

using namespace std;

int functionA(int *x){

     *x = 70;
     return *x;

}

int main()
{
   int x = 60;
   cout << x << endl;
   functionA(&x);
   cout << x << endl;





so for the first example,I'm passing by reference(memory location) of the integer variable number and it gets passed into the function functionA,now line 14 I thought I'm changing where x(address of number) is pointing to by assigning x to the address of y(&y) now by doing this I thought I would change the value of number to y which is 55 but yet this does not happen,I don't understand why the value does not change here.

thanks,

Adam
closed account (48bpfSEw)
1
2
3
4
5
int functionA(int *x){
  int y = 55;
  x = &y;
  return *x;
}



"int y" is a local variable!

Its address and value is only within the scope { } defined.
x is assigned to an address after the scope which no more exists.
It COULD show to 55 accidentally but probably not.

These are the bugs so hardly to find!

Check your code with cppChecker to find such errors!
somebody on another forum mentioned that I am not actually passing by reference but actually passing a copy of a pointer(passing by value) that makes the most sense so far but I always thought that when you pass a pointer or memory address into a function the value in the original pointer or memory address that you passed gets modified ??
could anybody clarify this for me I've been on this problem for days read chapters and chapters of books and never came accross a problem like this
closed account (E0p9LyTq)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>

// passes by pointer
void functionP(int* x);

// passes by reference
void functionR(int& x);

// your function changes the address pointed to
// the address is in scope only in the function
void yourFunction(int* x);


int main()
{
   int number = 60;
   std::cout << "In main():\n";
   std::cout << "number: " << &number << ", " << number << "\n\n";

   functionP(&number);
   std::cout << "In main():\n";
   std::cout << "number: " << &number << ", " << number << "\n\n";

   functionR(number);
   std::cout << "In main():\n";
   std::cout << "number: " << &number << ", " << number << "\n\n";

   // why does your function muck up?
   // the address pointed to is NOT valid in main()!
   yourFunction(&number);
   std::cout << "In main():\n";
   std::cout << "number: " << &number << ", " << number << "\n";
}


// passes by pointer
void functionP(int* x)
{
   int y = 155;
   *x = y;
   std::cout << "In functionP(int*):\n";
   std::cout << "y: " << y << ", " << &y << "\n";
   std::cout << "*x: " << *x << ", " << x << ", " << &x << "\n\n";
}


// passes by reference
void functionR(int& x)
{
   int y = 2355;
   x = y;
   std::cout << "In functionR(int&):\n";
   std::cout << "y: " << y << ", " << &y << "\n";
   std::cout << "&x: " << x << ", " << &x << "\n\n";
}


// your function changes the address pointed to
// the address is in scope only in the function
void yourFunction(int* x)
{
   int y = 55;
   x = &y;
   std::cout << "In yourFunction(int*):\n";
   std::cout << "y: " << y << ", " << &y << "\n";
   std::cout << "*x: " << *x << ", " << x << ", " << &x << "\n\n";
}


In main():
number: 00CFF9C4, 60

In functionP(int*):
y: 155, 00CFF8DC
*x: 155, 00CFF9C4, 00CFF8F0

In main():
number: 00CFF9C4, 155

In functionR(int&):
y: 2355, 00CFF8DC
&x: 2355, 00CFF9C4

In main():
number: 00CFF9C4, 2355

In yourFunction(int*):
y: 55, 00CFF8DC
*x: 55, 00CFF8DC, 00CFF8F0

In main():
number: 00CFF9C4, 2355
Topic archived. No new replies allowed.