Meaning of pass by reference in C and C++?

后端 未结 10 1074
生来不讨喜
生来不讨喜 2020-12-17 20:46

I am confused about the meaning of \"pass by reference\" in C and C++.

In C, there are no references. So I guess pass by reference means passing a pointer. But then

相关标签:
10条回答
  • 2020-12-17 21:47

    "Pass by reference" (or "call by reference") is a term for a type of parameter passing when calling a function, and the idea is older than C++. It does not necessarily have to be done using C++ "references". C doesn't have a built-in mechanism to do this, so you have to use pointers.

    0 讨论(0)
  • 2020-12-17 21:49

    In colloquial usage, "pass by reference" means that, if the callee modifies its arguments, it affects the caller, because the argument as seen by the callee refers to the value as seen by the caller.

    The phrase is used independent of the actual programming language, and how it calls things (pointers, references, whatever).

    In C++, call-by-reference can be done with references or pointers. In C, call-by-reference can only be achieved by passing a pointer.

    "Call by value":

    void foo( int x )
    {
        // x is a *copy* of whatever argument foo() was called with
        x = 42;
    }
    
    int main()
    {
        int a = 0;
        foo( a );
        // at this point, a == 0
    }
    

    "Call by reference", C style:

    void foo( int * x )
    {
        // x is still a *copy* of foo()'s argument, but that copy *refers* to
        // the value as seen by the caller
        *x = 42;
    }
    
    int main()
    {
        int a = 0;
        foo( &a );
        // at this point, a == 42
    }
    

    So, strictly speaking, there is no pass-by-reference in C. You either pass the variable by-value, or you pass a pointer to that variable by-value.

    0 讨论(0)
  • 2020-12-17 21:51

    In C, there are no any reference variables, but you can pass by reference with using pointers.

    In wikipedia, there is this definition. In call-by-reference evaluation (also referred to as pass-by-reference), a function receives an implicit reference to a variable used as argument, rather than a copy of its value. So this term is for type of parameter passing as mentioned by Thomas. So yes, since C is older than C++, also this idea is older than C++.

    However, in C++ both pointers and references can be used for passing to the function(Call by address and call by reference). Actually they are working the same way, they have only a few differences.

    • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
    • References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
    • A reference must be initialized when declared. There is no such restriction with pointers

    With these differences, if you use call by reference instead of call by pointer, you can reduce the possibility of NULL pointer error kind of problems.

    0 讨论(0)
  • 2020-12-17 21:52

    Just to add to the answers, referencing does not mean reference by address. The compiler may use any method to reference to a variable.

    0 讨论(0)
提交回复
热议问题