+ 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?
5 Antworten
+ 4
There is no difference as far as the position of * between DataType and VariableName
+ 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).
+ 2
No problem. As long as learning is achieved I'm happy. :-)
+ 1
Alright, thank you Stefan and Jigar for your answers!
+ 1
You are welcome :)