Pass By Reference - Question

I know the code below will print “123456”.

But I am curious on something.
In the ref function parameters it takes in *a which is a pointer. Why can’t it take &a instead? I’m just confused on the function parameter. Can someone explain the workings of it?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void ref(int *a)
{
*a=123456;
}


int main()
{
  int x = 123;
  ref(&x);
  cout << x;

}


It can, but at that point you're changing the function (and would need to call it differently, i.e. by not using the address-of operator on line 11).

-Albatross
The "&" operator is going to result in the memory address of the variable it's used on. In this case, it'll send the function "void ref" the memory address of 'x'. The "*" (used in the parameter) is declaring a variable that is able to hold the memory address of an integer variable.

If you were to instead make it (int &a) and leave the function call as (&x), you're trying to give a normal variable a memory address. The variable isn't meant to be given a memory address. Instead, (int &a) signals that "a" shouldn't be allocated new memory for, but instead should use the same memory of the variable in the function call. EX:

1
2
3
4
5
6
7
8
9
10
11
12
void ref(int *a)
{
	std::cout << a;
}


int main()
{
	int x = 123;
	ref(&x);
	std::cout << '\n' << &x;
}


Has the Same output as:

1
2
3
4
5
6
7
8
9
10
11
12
void ref(int &a)
{
	std::cout << &a;
}


int main()
{
	int x = 123;
	ref(x);
	std::cout << '\n' << &x;
}



Each bit of code will output two memory addresses which will be the same.
Pass By Reference
void ref(int*);

That is not by reference. That parameter is passed by value.
yea, you need to watch that, its a new coder trap.
if you pass a pointer by value, it can still modify the data pointed to, but the pointer can't change.

that is...

void foo (int * ip)
{
ip = new int[100]; //memory leaked. local ip / pointer
// memory is discarded at function end. ip reverts to what was passed in.
}

void foo (int *&ip)
{
ip = new int[100]; //correct, value passed in is modified.
}

but
void foo (int * ip)
{
ip[0] = 13; //this change sticks outside the function.
//the pointer was passed by value, but the memory where it points is free for all territory.
}

read these 3 until it sticks. Its critical if you are going to be fooling with pointers.

pointers can act like references, with a lot of overlap, made worse by the way & is used for both types. That is also something you want to get used to, and also critical. They are not the same thing, even if they sometimes do the same things.
Last edited on
If you're asking about when a parameter should be a pointer and when should it be a reference, there's an easy (to me anyway) rule of thumb: if it's legal to pass nullptr then pass a pointer. If you require an actual object, then pass a reference.

Most of the time you'll find that you want a reference. This makes it obvious that the function requires an object. It also makes it hard (but not impossible) to mistakenly pass a "null object".
Topic archived. No new replies allowed.