parameter passing mechanisms
can someone tell me why and when one of the following is prefered over the other
pass by reference using reference arguments
pass by reference using pointer arguments
Both are insecure (unless const is used to qualify the parameters)
but dont both of them essentially have the same dynamics ?? so why is one prefered over the other ??
You can't do pointer arithmetic on references. For example, if I pass a function a pointer to a single character, there's nothing to stop that function treating the argument as an array of characters. This couldn't happen if the function required a reference to a character instead of a pointer to a character.
I agree with the previous post.
Pros with passing References:
When passing a reference, the code inside of the function treats the reference just like a global variable - that is...no need to use * operators and changes to the reference changes the passed variable.
Cons with passing References:
References are not pointers and cannot be manipulated
via pointer arithmatic.
|All times are GMT -5. The time now is 08:45 PM.|