passing a value or passing an address
2019-02-26 19:00:002019-03-26 19:00:00
|I just wanted to make sure....soe C pass a parameter by value or by address....
According to below example I say "by value".
Let's have a look at the code:
[martijn@radijs misc]$ cat param.c #includeNow compile it:
[martijn@radijs misc]$ gcc -o param param.cAnd finally run it:
[martijn@radijs misc]$ ./param main : a :0 <-- a has a value of 0 change_param_a : a : 0 <-- within param_a a starts with a value of 0 change_param_a : b : 1 <-- then, as expected, a ha a value of 1 main : b : 0 <-- however...back in main a has a value of 0 change_param_b : a : 0 <-- within param_b starts with a value of 0 change_param_b : b : 1 <-- The value is increased by the function main : c : 1 <-- back in main a is still 1 [martijn@radijs misc]$So....my conclusion: c passes parameters by value. As we can see in function param_a() the value of a is passed. Whitin the function this value is altered. When returned back to the main program the original value is still there. Only in the function the value was changed. When using param_b() the address of a is passed. The function then alters the value of the integer stored at the passed address. This is (ofcourse) seeable for the mainprogram, since the whole program sees the same address-space.