+ 3
Both serve same purpose. Using reference will make alias name. Using pointer will make a new pointer variable which can be reassigned.. Edit: Hope this helps, for more details. https://hackr.io/blog/pass-by-reference-vs-pass-by-pointer
11th Jun 2022, 1:27 PM
Jayakrishna 🇼🇳
+ 2
That was not address-of operator, that was the aymbol to signify that <a> and <b> are `int` references. Read here for difference between pointers and references. https://techdifferences.com/difference-between-pointer-and-reference-2.html In regards to parameters, no operator is expected, cause arguments are to be used (and occasionally modified), but not to be evaluated (one of the purpose of operators - to evaluate and return).
11th Jun 2022, 11:56 AM
Ipang
+ 2
Hope this example helps... #include <iostream> using namespace std; void sum(int &a, int &b){ a = 12, b =15; } int main() { int a = 3, b = 5; cout << a << b <<endl; sum(a,b); cout << a << b; return 0; } Actual parameter value gets stores in formal parameter address so that formal parameter changes reflect back on actual parameters.. It's like pass by reference.
11th Jun 2022, 12:11 PM
Jayakrishna 🇼🇳
+ 1
Yes. It now deal with pointers.. #include <iostream> using namespace std; void sum(int *a, int *b){ *a = 12, *b =15; } int main() { int a = 3, b = 5; cout << a << b <<endl; sum(&a,&b); cout << a << b; return 0; }
11th Jun 2022, 12:54 PM
Jayakrishna 🇼🇳
+ 1
Manav Roy When we see & symbol in parameters list, it defines that the parameter is expected as a reference. When we see & symbol inside the function body, global space (anywhere but the parameters list), then it could either be address-of operator (unary - with one operand), or binary AND operator (binary - with two operands on its left and right side). Pointers came with C, references came with C++. C recognizes pointers but not references. C++ recognises both pointers and references. More about the differences between pointers and references, you may find in the link in my earlier post.
11th Jun 2022, 1:57 PM
Ipang
- 1
Skksk
11th Jun 2022, 2:56 PM
Pravesh Chhipi