#include <iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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.
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 ??
#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