在我大学的C编程课上,教授和她后来写的书在提到C中的pointers时使用了术语call or pass by reference.

我的教授认为这是一个"按引用调用函数"的例子:

int sum(int *a, int *b);

我的教授将其视为"价值函数调用"的一个例子:

int sum(int a, int b);

我的readC不支持按引用调用.据我所知,是pointers pass by value.

基本上,说指针是C的引用传递方式是incorrect吗?在C中不能通过引用传递,但可以使用指针作为替代,这会是more correct吗?


Update 11/11/15

从我提出问题的方式来看,我认为一场术语辩论已经结束,事实上,我看到了两个具体的区别.

  • 传递引用(今天主要使用的术语):specific个术语,用于C++语言
  • 按引用传递(我的教授将其用作解释指针的范例):这是C++等语言开发之前使用的general个术语,因此在该术语被重写之前也是如此

在阅读了@Haris的最新答案后,有理由解释为什么这不是那么黑和白.

推荐答案

you cannot pass by reference in C but can use pointers as an alternative

是的,没错.


再详细一点.无论您将什么作为参数传递给c函数,它都只通过值传递.无论是变量的值还是变量的地址.

What makes the difference is what you are sending.

When we pass-by-value we are passing the value of the variable to a function. When we pass-by-reference we are passing an alias of the variable to a function. C can pass a pointer into a function but that is still pass-by-value. It is copying the value of the pointer, the address, into the function.


  • 如果要发送变量的值,则函数将只接收该值,更改该值不会影响原始值.

  • 如果要发送变量的地址,则也只发送值(在本例中为地址),但是由于您拥有变量的地址,因此可以使用它来更改原始值.


例如,我们可以看到一些C++代码来理解按值调用和按引用调用之间的真正区别.摘自this网站.

// Program to sort two numbers using call by reference. 
// Smallest number is output first.

#include <iostream>
using namespace std;

// Function prototype for call by reference
void swap(float &x, float &y);

int main()
{
   float a, b;

   cout << "Enter 2 numbers: " << endl;
   cin >> a >> b;
   if(a>b) 
     swap(a,b); // This looks just like a call-by-value, but in fact
                // it's a call by reference (because of the "&" in the
                // function prototype

   // Variable a contains value of smallest number
   cout << "Sorted numbers: ";
   cout << a << " " << b << endl;
   return 0;
}

// A function definition for call by reference
// The variables x 和 y will have their values changed.

void swap(float &x, float &y)
// Swaps x 和 y data of calling function
{
   float temp;

   temp = x;
   x = y;
   y = temp;
}

在这个C++示例中,使用了reference variable(C中不存在).引用this个网站,

"A reference is an alias, or an alternate name to an existing variable...",

"The main use of references is acting as function formal parameters to support pass-by-reference..."

这与使用指针作为函数参数不同,因为,

"A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address."

So, essentially when one is sending address 和 receiving through pointers, one is sending the value only, but when one is sending/receiving a reference variable, one is sending an alias, or a reference.


**UPDATE : 11 November, 2015**

There has been a long debate in the C Chatroom, 和 after reading comments 和 answers to this question, i have realized that there can be another way to look at this question, another perspective that is.

让我们看一些简单的C代码

int i;
int *p = &i;
*p = 123;

In this scenario, one can use the terminology that, p's value is a reference to i. So, if that is the case, then if we send the same pointer (int* p) to a function, one can argue that, since i's reference is sent to the function, 和 thus this can be called pass-by-reference.

So, its a matter of terminology 和 way of looking at the scenario.

I would not completely disagree with that argument. But for a person who completely follows the book 和 rules, this would be wrong.


NOTE:更新灵感来自this聊天.

C++相关问答推荐

为什么PLT表中没有push指令?

InetPton()函数无效的IP地址

找出文件是否包含给定的文件签名

在C中将通用字符名称转换为UTF-8

从纯C中访问通用项对话框

Clang警告称,`ffi_type`与`sizeof`不兼容

GCC不顾-fno-Builtin-SINCOS旗帜向SINCOS发出呼唤

在Linux上使用vscode和lldb调试用Makefile编译的c代码

如何仅使用软件重新初始化STM32微控制器中的USB枚举?

将字符串数组传递给C中的函数:`str[dim1][str_size]`vs`*str[dim1]`

我在C中运行和调试时得到了不同的输出

C程序向服务器发送TCPRST

对于STM32微控制器,全局偏移表.get和.Got.plt必须为零初始化

C++中PUTS函数的返回值

如何在C中处理流水线中的a、n命令?

被调用方函数内部的C-Struct变量,它是指针还是无关紧要

可以对两种 struct 类型中的任何一种进行操作的C函数

`%%的sscanf无法按预期工作

与指针的原始C数组或C++向量<;向量<;双>>;

快速准确计算double的小数指数