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

后端 未结 10 1073
生来不讨喜
生来不讨喜 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:26

    A reference in general is an instance that is referencing something else. Thus in a wider sense, also a pointer can be considered as one possible implementation of a reference. References in C++ are just called references, because apart from referencing something they offer no other features.

    Pass-by-reference is used in general to distinguish from pass-by-value. Whether it is via pointer or via a reference is often just a minor detail. However, with C++ references it is imho more clear what is the purpose of the function parameter. Eg:

    int foo(int& a);         // pass-by-reference
    int foo(const int& a);   // is pratically pass-by-value 
                             // (+ avoiding the copy of the parameter)
    

    on the other hand, with references (as compared to pointers) it is not so obvious at the call site if it is pass-by-value or pass-by-reference. E.g.

    int x;
    int y = foo(x);  // could be pass-by-value or pass-by-reference
    int z = foo(&x); // obviously pass-by-reference (as a pointer)
    
    0 讨论(0)
  • 2020-12-17 21:27

    In C there are two concepts

    1. Call by value - Here copy of values are passed so actual values will not change outside the function.

    2. Call by reference - but here actual values (Address of actual operands) are passed so it will change the values globally.

    Where in C++ there are two concepts

    1. Pass by value - it is same as c, actual values will not change, scope of this values are of function only.

    2. Pass by Reference - actual values (Address of actual operands) are passed so it will change the values globally, it means if values gets changed then it will affect in whole program.

    In Pass by Reference, the address of operands are passed that's why it is called as Pass By Reference not as pointer.

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

    Imagine you have to paint your house...

    by value: you bring a copy of your house to the painter => much effort (maybe on rails)
    by reference: you give your house address to the painter so he can come and paint it

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

    In C, there are no references

    There are no reference variables. But you can refer to objects using pointers. Therefore pointers are "references" from an abstract point of view.

    But then why not call it pass by pointer?

    You can call it pass by pointer. Reference is a more general term than pointer. It is often preferable to use the more general term when you want to discuss abstractions and want to ignore implementation details. You would call it pass by reference for the same reason that you call a variable "integer" rather than "int32_t".

    In C++, we have both pointers and references (and stuff like iterators that lies close). So what does pass by reference mean here?

    Depends on context. Often it means that the function argument is a reference variable, but it may also refer to a pointer, iterator, a reference wrapper... anything that referes to an object.


    Reference is an abstract concept that exists beyond c and c++; even beyond programming. In c++, the term is ambiguous with reference variables and the context and convention (which isn't universal) determines the meaning.

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

    when you pass something by reference you're working with the address and not the value of a variable directly, If you use a reference parameter you're getting the address of the variable you pass in.

    From there you can manipulate it how ever you want as the variable you passed in WILL change if you change the reference in the function. It's an easier way to work with large amounts of a data it really just saves on memory etc..

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

    Let's clear your confusion.

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

    Because every argument passing in C is pass-by-value. Even a pointer argument is a copy. But it contains (or points to, if you prefer) the same value -- memory address. That is how you can change the variable it points to, but not the pointer itself. Since it's a copy, whatever you do will not affect the pointer on the caller level.

    In C++, we have both pointers and references (and stuff like iterators that lies close). So what does pass by reference mean here?

    It means, that the argument is an alias of a variable on the caller level, not a copy, which allows us to change it.

    Hope that helped.

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