I understand calling foo(value) fine, but how does calling foo(*p) produce the same results? I thought it would error, but it works just fine.
*p is dereferencing the pointer, getting the value at address stored in p, so isn't it equivalent to calling foo(5)?
void foo(int &ptr)
{
ptr = 6;
}
int main()
{
using namespace std;
int value = 5;
int *p = &value;
cout << "value = " << value << '\n';
foo(*p);
cout << "value = " << value << '\n';
return 0;
}
*p = 7;*pgets the value thatppoints to. But you pass that value by reference, so the function gets a reference to the value thatppoints to.