I wrote following code using int* input
and int& input
and both works same. What is difference and what is right approach to pass reference?
#include<iostream>
void increment(int* input);
int main()
{
int a = 34;
std::cout<<"Before the function call a = "<<a<<"\n";
increment(&a);
std::cout<<"After the function call a = "<<a<<"\n";
return 0;
}
void increment(int * input)
{
(*input)++;
std::cout<<"In the function call a = "<<*input<<"\n";
}
Following code also works fine.
#include<iostream>
void increment(int &input);
int main()
{
int a = 34;
std::cout<<"Before the function call a = "<<a<<"\n";
increment(a);
std::cout<<"After the function call a = "<<a<<"\n";
return 0;
}
void increment(int &input)//Note the addition of '&'
{
input++; //**Note the LACK OF THE addition of '&'**
std::cout<<"In the function call a = "<<input<<"\n";
}
Any idea?