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

  1. call-by-value
  2. call-by-address (pointer)
  3. call-by-alias
  4. global variable / static variable

if please give example, appreciate that, , work commended.

  1. 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.

  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 of myvalue 4 @ end of main().

  3. call-by-alias

    there no alias in c per understanding. should c++ reference mechanism.

  4. 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 of main().

hope helps.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -