Pass by reference in C

后端 未结 7 1577
夕颜
夕颜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 03:58

    (I am have been using C for a while but I am pretty still new to few concepts in C. I am trying to answer this to the best of my knowledge, feel free to correct me If I am wrong)

    When you say pass by reference you pass the address, and do to so you would use pointers pointing to the addresses. So you need to pass the pointers to your function prototype but what you are doing is passing address directly.

    What your function tries is de-reference(using &) your pointer, but you never had a pointer to parameters 's' and 'i' to de-reference it.

    The right way to do for the function prototype/definition is

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

    and when you have to call this function you use the below declaration in the main or calling function

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

提交回复
热议问题