"*&"
int *&x
int *y
is there any difference between these two variables?
Yes.
x is a reference to a int pointer
y is an int pointer
If you use y as a function parameter, the function will receive y as a a copy of the passed in pointer..
However if you use x as a function parameter, the function will receive x as a reference to the passed in pointer.
This means that if the value of y is changed inside the function it will not be changed outside the function.
But if x is changed inside the function it will also be changed outside the function.
Here is an example:
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
|
void func(int* p, int*& pr)
{
p++;
pr++;
}
int main()
{
int a[2];
int* b = &a[0];
int* c = &a[0];
std::cout << "\nBefore call to function:" << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
func(b, c);
std::cout << "\nAfter call to function:" << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
return 0;
}
|
And here is the output:
Before call to function:
b = 0xbf811a48
c = 0xbf811a48
After call to function:
b = 0xbf811a48
c = 0xbf811a4c
Last edited on
thanks alot Galik!
Topic archived. No new replies allowed.