+ 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; }
3 Respostas
+ 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.
+ 6
You may have to elaborate on your query, or provide an example code.
+ 2
thank you very much