Passing by reference in C

后端 未结 17 2213
梦如初夏
梦如初夏 2020-11-21 23:26

If C does not support passing a variable by reference, why does this work?

#include 

void f(int *j) {
  (*j)++;
}

int main() {
  int i = 20;         


        
17条回答
  •  不思量自难忘°
    2020-11-21 23:44

    Because there is no pass-by-reference in the above code. Using pointers (such as void func(int* p)) is pass-by-address. This is pass-by-reference in C++ (won't work in C):

    void func(int& ref) {ref = 4;}
    
    ...
    int a;
    func(a);
    // a is 4 now
    

提交回复
热议问题