Passing by reference in C

后端 未结 17 2137
梦如初夏
梦如初夏 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:50

    Because you're passing the value of the pointer to the method and then dereferencing it to get the integer that is pointed to.

    0 讨论(0)
  • 2020-11-21 23:52

    Short answer: Yes, C does implement parameter passing by reference using pointers.

    While implementing parameter passing, designers of programming languages use three different strategies (or semantic models): transfer data to the subprogram, receive data from the subprogram, or do both. These models are commonly known as in mode, out mode, and inout mode, correspondingly.

    Several models have been devised by language designers to implement these three elementary parameter passing strategies:

    Pass-by-Value (in mode semantics) Pass-by-Result (out mode semantics) Pass-by-Value-Result (inout mode semantics) Pass-by-Reference (inout mode semantics) Pass-by-Name (inout mode semantics)

    Pass-by-reference is the second technique for inout-mode parameter passing. Instead of copying data back and forth between the main routine and the subprogram, the runtime system sends a direct access path to the data for the subprogram. In this strategy the subprogram has direct access to the data effectively sharing the data with the main routine. The main advantage with this technique is that its absolutely efficient in time and space because there is no need to duplicate space and there is no data copying operations.

    Parameter passing implementation in C: C implements pass-by-value and also pass-by-reference (inout mode) semantics using pointers as parameters. The pointer is send to the subprogram and no actual data is copied at all. However, because a pointer is an access path to the data of the main routine, the subprogram may change the data in the main routine. C adopted this method from ALGOL68.

    Parameter passing implementation in C++: C++ also implements pass-by-reference (inout mode) semantics using pointers and also using a special kind of pointer, called reference type. Reference type pointers are implicitly dereferenced inside the subprogram but their semantics are also pass-by-reference.

    So the key concept here is that pass-by-reference implements an access path to the data instead of copying the data into the subprogram. Data access paths can be explicitly dereferenced pointers or auto dereferenced pointers (reference type).

    For more info please refer to the book Concepts of Programming Languages by Robert Sebesta, 10th Ed., Chapter 9.

    0 讨论(0)
  • 2020-11-21 23:55

    In C, to pass by reference you use the address-of operator & which should be used against a variable, but in your case, since you have used the pointer variable p, you do not need to prefix it with the address-of operator. It would have been true if you used &i as the parameter: f(&i).

    You can also add this, to dereference p and see how that value matches i:

    printf("p=%d \n",*p);
    
    0 讨论(0)
  • 2020-11-21 23:55

    pointers and references are two different thigngs.

    A couple of things I have not seen mentioned.

    A pointer is the address of something. A pointer can be stored and copied like any other variable. It thus have a size.

    A reference should be seen as an ALIAS of something. It does not have a size and cannot be stored. It MUST reference something, ie. it cannot be null or changed. Well, sometimes the compiler needs to store the reference as a pointer, but that is an implementation detail.

    With references you don't have the issues with pointers, like ownership handling, null checking, de-referencing on use.

    0 讨论(0)
  • 2020-11-21 23:58

    Your example works because you are passing the address of your variable to a function that manipulates its value with the dereference operator.

    While C does not support reference data types, you can still simulate passing-by-reference by explicitly passing pointer values, as in your example.

    The C++ reference data type is less powerful but considered safer than the pointer type inherited from C. This would be your example, adapted to use C++ references:

    void f(int &j) {
      j++;
    }
    
    int main() {
      int i = 20;
      f(i);
      printf("i = %d\n", i);
    
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题