function - Parameter Passing in C - Pointers, Addresses, Aliases -
could please explain difference between parameter passing in c please? according professor notes there 4 different ways pass parameters
- call-by-value
- call-by-address (pointer)
- call-by-alias
- global variable / static variable
if please give example, appreciate that, , work commended.
call-by-value
passing value function parameter. if function modifies variable, actual variable won't changed.
void fun1(int myparam) { myparam = 4; } void main() { int myvalue = 2; fun1(myvalue); printf("myvalue = %d",myvalue); }
myvalue
2.call-by-address (pointer)
void fun1(int *myparam) { *myparam = 4; } void main() { int myvalue = 2; fun1(&myvalue); printf("myvalue = %d",myvalue); }
here passing address of
myvalue
fun1
. value ofmyvalue
4 @ end ofmain()
.call-by-alias
there no alias in c per understanding. should c++ reference mechanism.
global variable / static variable
global , static variables variables stored in common places, accessible caller , callee functions. both caller , callee able access , modify them.
int myvalue = 2; void fun1() { myvalue = 4; } void main() { myvalue = 2 fun1(); printf("myvalue = %d",myvalue); }
as can guess, value of
myvalue
4 @ end ofmain()
.
hope helps.
Comments
Post a Comment