+ 1

Passing by reference - difference

What's the difference between these functions: void function(int* var) void function(int * var) void function(int *var) I've seen these three types of pointer but don't understand the difference. Can anybody explain?

23rd Jun 2016, 2:23 AM
Asad
5 ответов
+ 4
There is no difference as far as the position of * between DataType and VariableName
23rd Jun 2016, 2:57 AM
Jigar Thacker
Jigar Thacker - avatar
+ 2
Hi Asad, this is not what is usually referred to by "pass by reference". Passing by reference uses the "&" operator instead of "*". Nevertheless, it's pretty similar to passing a pointer by value (this is what we got here) and can be considered a simpler way of passing a pointer to an *existing* variable (important restrictions as you cannot pass a null reference while you can pass a dangerous null pointer by value). The difference in your example is one of opinion (that means there is views on this in the programmers community). While "int* var" suggests that the quality of being a pointer is a property of the type, "int *var" suggests it's a property of the variable "var". "int * var" leaves it open (sometimes used by code refactoring tools to leave it open to automatic formatter or so).
23rd Jun 2016, 5:08 AM
Stefan
Stefan - avatar
+ 2
No problem. As long as learning is achieved I'm happy. :-)
23rd Jun 2016, 11:48 AM
Stefan
Stefan - avatar
+ 1
Alright, thank you Stefan and Jigar for your answers!
23rd Jun 2016, 10:50 AM
Asad
+ 1
You are welcome :)
23rd Jun 2016, 10:53 AM
Jigar Thacker
Jigar Thacker - avatar