指针是否被认为是C语言中通过引用进行调用的方法? [英] Are pointers considered a method of calling by reference in C?

查看:107
本文介绍了指针是否被认为是C语言中通过引用进行调用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我大学的C编程课程中,教授和她所写的后续著作在引用C中的 pointers 时使用了调用或引用传递一词.

In my University's C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to pointers in C.

我的教授将其视为按引用调用"的示例:

An example of what is considered a 'call by reference function' by my professor:

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

我的教授将其视为按价值调用功能"的示例:

An example of what is considered a 'call by value function' by my professor:

int sum(int a, int b);

我已经阅读 C不支持引用调用.据我了解,指针按值传递.

I've read C doesn't support call by reference. To my understanding, pointers pass by value.

基本上,说指针是C的引用传递方式不正确吗?说您不能在C中通过引用传递但可以使用指针替代,这会更正确吗?

Basically, is it incorrect to say pointers are C's way of passing by reference? Would it be more correct to say you cannot pass by reference in C but can use pointers as an alternative?

更新15/11/11

从我提出问题的方式出发,我相信对术语的争论已经开始,实际上,我看到了两个特定的区别.

From the way my question originated, I believe a debate of terminology has stemmed and in fact I'm seeing two specific distinctions.

  • 通过引用传递(今天主要使用该术语):像C ++这样的语言中使用的特定术语
  • 按引用传递(我的教授用作解释指针的范式): general 术语,是在开发C ++之类的语言之前并因此在对其进行重写之前使用的.
  • pass-by-reference (the term used mainly today): The specific term as used in languages like C++
  • pass-by-reference (the term used by my professor as a paradigm to explain pointers): The general term used before languages like C++ were developed and thus before the term was rewritten

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

After reading @Haris' updated answer it makes sense why this isn't so black and white.

推荐答案

您不能在C中通过引用传递,但可以使用指针作为替代

是的,那是正确的.

详细说明.无论您将什么作为参数传递给c函数,它都仅由值传递.无论是变量的值还是变量的地址.

To elaborate a little more. Whatever you pass as an argument to c functions, it is passed by values only. Whether it be a variable's value or the variable address.

与众不同的是您要发送的内容.

传递值时,我们会将变量的值传递给函数.当我们通过引用传递时,我们会将变量的别名传递给函数. C可以将指针传递到函数中,但仍然是按值传递.它将指针的值(地址)复制到函数中.

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

  • If you are sending the value of a variable, then only the value will be received by the function, and changing that won't effect the original value.

如果要发送变量的地址,则也仅发送值(在这种情况下为地址),但是由于您具有变量的地址,因此可以用来更改原始值.

If you are sending the address of a variable, then also only the value(the address in this case) is sent, but since you have the address of a variable it can be used to change the original value.

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

As an example, we can see some C++ code to understand the real difference between call-by-value and call-by-reference. Taken from this website.

// 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 and y will have their values changed.

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

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

在此C ++示例中,使用了参考变量(C中不存在).引用网站

In this C++ example, reference variable(which is not present in C) is being used. To quote this website,

引用是别名,还是现有变量的替代名称..."

引用的主要用途是作为函数形式参数来支持传递引用..."

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

This is different then the use of pointers as function parameters because,

指针变量(或简称指针)与其他变量基本相同,其他变量可以存储一段数据.与普通变量不同,该变量存储一个值(例如int,double,a char),则指针将存储内存地址."

因此,基本上,当一个人正在发送地址并通过指针接收时,一个人仅在发送值,而当一个人在发送/接收一个引用变量时,一个人正在发送别名或引用.


更新:2015年11月11日

C聊天室进行了长时间的辩论,在阅读了对该问题的评论和答案后,我已经意识到,可以用另一种方式来看这个问题,那就是另一种观点.

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

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

Lets look at some simple C code

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

在这种情况下,可以使用以下术语,即 p的值是对i的引用.因此,如果是这种情况,那么如果我们将相同的指针(int* p)发送给一个函数,则可以认为,由于i的引用已发送给该函数,因此可以将其称为通过引用.

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, and thus this can be called pass-by-reference.

因此,这只是术语和查看场景的方式.

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

我不会完全不同意这种说法.但是对于一个完全遵循这本书和规则的人来说,这是错误的.

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

注意:更新受聊天的启发.

这篇关于指针是否被认为是C语言中通过引用进行调用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆