Pass by reference in C

后端 未结 7 1522
夕颜
夕颜 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 04:13

    C does not have references. You need to pass a pointer to the variable you wish to modify:

    int locate(char *name, int *s, int *i)
    {
        /* ... */
    
        *s = 123;
        *i = 456;
    }
    
    int s = 0;
    int i = 0;
    locate("GMan", &s, &i);
    
    /* s & i have been modified */
    
    0 讨论(0)
提交回复
热议问题