Pass by reference in C

后端 未结 7 1545
夕颜
夕颜 2020-12-03 03:34

I\'m trying to use pass by reference in C so that the function can modify the values of the parameters passed to it. This is the function signature:

int loc         


        
7条回答
  •  遥遥无期
    2020-12-03 03:57

    If you wish to use pass by reference method to modify a parameter used in function call, always use pointers as formal arguments of function.

    Hence instead of using this

    int locate(char *name, int &s, int &i)
    

    use,

    int locate(char *name, int *s, int *i)
    

    When you will call this function, you may use

    .........

    int a=5;
    int d=10;
    char c='A'; 
    int result;
    
    result=locate(&c, &a, &d);
    

    I hope all would be clear now,

    For more idea about passing by value or passing by reference and which one is better, this source is good for beginners

    http://www.learnc.net/call-by-reference.php

    Have fun.

提交回复
热议问题