Function arguments, pass by pointer or reference?
|
Newbie Member
|
|
| 20Feb2011,13:28 | #1 |
|
Which do you think is best? or cons/pros of each of passing by pointer vs passing by reference.
|
|
Invasive contributor
|
![]() |
| 21Feb2011,13:00 | #2 |
|
Quote:
Originally Posted by hseldon Passing by reference allows us to safeguard our data... Now lets test it :- function.c Code: c
Compiling Code:
gcc function.c -o function Code:
aneesh@aneesh-laptop:~/Programming/C$ ./function Input = 1337 Input = 100 Input = 100 |
|
Go4Expert Founder
|
![]() |
| 21Feb2011,13:46 | #3 |
|
Quote:
Originally Posted by lionaneesh |
|
Mentor
|
![]() |
| 21Feb2011,15:22 | #4 |
|
Neither is better; they are different tools for different jobs. You may as well ask which is better out of a hammer and a screwdriver.
|
|
Invasive contributor
|
![]() |
| 21Feb2011,15:59 | #5 |
|
Newbie Member
|
|
| 3Mar2011,11:04 | #6 |
|
There really is no best, you have to know why you're passing by that choice when you create the function. One advantage of pass by pointer is you can set them to NULL to represent different things in your function while references have to point to an object even though that object might not be valid.
Allen LeVan UAT Student |




