+ 1

Does pass by reference only use pointers and addresses ?

void passbyRef(int *x); int main{ int boy = 13; passbyRef(&boy); cout<<"boy is now "<<endl; } void passbyRef(int *x){ *x = 23; }

14th Jan 2017, 10:46 AM
Eli Amevor
Eli Amevor - avatar
3 ответов
+ 8
You will have to modify your code a bit #include <iostream> using namespace std; void passbyReference(int *x); int main() { int boy = 13; passbyReference(&boy); cout << "boy is now " << boy; return 0; } void passbyReference(int *x) { *x = 23; } What happens here is that the function accepts either a pointer, or an address as an argument, and the function will modify the values of the pointer which points to that address. In this example above, I passed the address of boy using &boy. You may also use a pointer to store address of boy and pass the pointer.
14th Jan 2017, 11:12 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
You may have to elaborate on your query, or provide an example code.
14th Jan 2017, 10:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
thank you very much
14th Jan 2017, 11:17 AM
Eli Amevor
Eli Amevor - avatar