Passing by reference in C

2018-12-31 05:05发布

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

#include <stdio.h>

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

int main() {
  int i = 20;
  int *p = &i;
  f(p);
  printf("i = %d\n", i);

  return 0;
}

Output

$ gcc -std=c99 test.c
$ a.exe
i = 21 

17条回答
初与友歌
2楼-- · 2018-12-31 05:21

p is a pointer variable. Its value is the address of i. When you call f, you pass the value of p, which is the address of i.

查看更多
唯独是你
3楼-- · 2018-12-31 05:21

I think C in fact supports pass by reference.

Most languages require syntactic sugar to pass by reference instead of value. (C++ for example requires & in the parameter declaration).

C also requires syntactic sugar for this. It's * in the parameter type declaration and & on the argument. So * and & is the C syntax for pass by reference.

One could now argue that real pass by reference should only require syntax on the parameter declaration, not on the argument side.

But now comes C# which does support by reference passing and requires syntactic sugar on both parameter and argument sides.

The argument that C has no by-ref passing cause the the syntactic elements to express it exhibit the underlying technical implementation is not an argument at all, as this applies more or less to all implementations.

The only remaining argument is that passing by ref in C is not a monolithic feature but combines two existing features. (Take ref of argument by &, expect ref to type by *.) C# for example does require two syntactic elements, but they can't be used without each other.

This is obviously a dangerous argument, cause lots of other features in languages are composed of other features. (like string support in C++)

查看更多
情到深处是孤独
4楼-- · 2018-12-31 05:22

What you are doing is pass by value not pass by reference. Because you are sending the value of a variable 'p' to the function 'f' (in main as f(p);)

The same program in C with pass by reference will look like,(!!!this program gives 2 errors as pass by reference is not supported in C)

#include <stdio.h>

void f(int &j) {    //j is reference variable to i same as int &j = i
  j++;
}

int main() {
  int i = 20;
  f(i);
  printf("i = %d\n", i);

  return 0;
}

Output:-

3:12: error: expected ';', ',' or ')' before '&' token
             void f(int &j);
                        ^
9:3:  warning: implicit declaration of function 'f'
               f(a);
               ^
查看更多
人气声优
5楼-- · 2018-12-31 05:23

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.

查看更多
唯独是你
6楼-- · 2018-12-31 05:24

Because you're passing a pointer(memory address) to the variable p into the function f. In other words you are passing a pointer not a reference.

查看更多
素衣白纱
7楼-- · 2018-12-31 05:27

You're passing a pointer(address location) by value.

It's like saying "here's the place with the data I want you to update."

查看更多
登录 后发表回答