Passing by reference/using pointers
I've just started C++ and am trying to get to grips with pointers, consider the following 2 programs:
Code:
void change(int &i)
{
i = 5;
}
int main()
{
int i = 0;
change(i);
printf("%d\n",i);
}
Code:
void change(int *i)
{
*i = 5;
}
int main()
{
int i = 0;
change(&i);
printf("%d\n",i);
}
The first one passes the reference of i, and the second passes up a pointer to i(correct me if I'm wrong)
What is the difference between the two methods of passing variables? As far as I can see, both edit the value of i directly. Why are pointers more useful?
Thanks