简体   繁体   English

C 中结构的表达式“通过引用传递”

[英]Expression “passing by reference” for structs in C

I have recently had a class about structures in C and the lecturer used the expression "pass by reference" to describe the action of passing a pointer towards a structure to a function.我最近有一个关于 C 中的结构的 class,讲师使用“通过引用传递”来描述将指向结构的指针传递给 ZC1C425268E68385D1AB5074C17A94F14 的操作。 I have always read that C does not have "pass by reference" because, unlike Cpp, pass by reference can only be emulated by passing by value a pointer towards some object — and we should rather say "pass by pointer".我一直读到 C 没有“按引用传递”,因为与 Cpp 不同,按引用传递只能通过按值传递指向某个 object 的指针来模拟——我们宁愿说“按指针传递”。 So I am wondering whether or not things work differently for structures and this expression is justified in this context.所以我想知道结构的工作方式是否有所不同,并且这种表达方式在这种情况下是合理的。

So I am wondering whether or not things work differently for structures and this expression is justified in this context.所以我想知道结构的工作方式是否有所不同,并且这种表达方式在这种情况下是合理的。

If you describe passing an int * as "pass by pointer" then you should also describe passing a struct foo * as "pass by pointer".如果您将传递int *描述为“按指针传递”,那么您还应该将传递struct foo *描述为“按指针传递”。 There is nothing special about structures that would require or even suggest different word choice than for other kinds of objects.与其他类型的对象相比,结构需要甚至建议选择不同的词,并没有什么特别之处。

In practice, there is a diversity of actual word usage in this area, but I am among those who argue against using the term "pass by reference" to describe passing pointers.在实践中,该领域的实际单词用法多种多样,但我属于反对使用术语“通过引用传递”来描述传递指针的人之一。 I think there are good reasons for that, but they would be tangential.我认为这是有充分理由的,但它们将是切线的。

The pass by value and pass by reference are the common wordings when we speak of passing parameters to subroutines or functions in a language agnostic way.当我们谈到以与语言无关的方式将参数传递给子例程或函数时,按值传递和按引用传递是常见的措辞。

The former says that the caller will never see changes to the variables and is used for input only data, the latter says that the caller will see the changes and is used for input/output or output only data.前者表示调用者永远不会看到变量的变化并且仅用于输入数据,后者表示调用者将看到变化并且仅用于输入/输出或 output 数据。

In C language, as the language only allow by value , the by reference way is emulated by using pointers, but it is an implementation detail and the goal is indeed to have pass by reference semantics.在 C 语言中,由于语言只允许按值按引用的方式是用指针模拟的,但它是一个实现细节,目标确实是通过引用语义。

For your precise question, nothing is special when it comes to structures, and it you want by reference semantics, you will have to pass a pointer.对于您的确切问题,在结构方面没有什么特别的,并且您想要通过引用语义,您将必须传递一个指针。

The only special thing for structure, is that you can return a structure from a function, what was not allowed in the first K&R C versions in the 70's or early 80's.结构的唯一特殊之处在于,您可以从 function 返回结构,这在 70 年代或 80 年代初的第一个 K&R C 版本中是不允许的。

Per the C standard, a pointer “provides a reference” (C 2018 6.2.5 20).根据 C 标准,指针“提供参考”(C 2018 6.2.5 20)。 This is a common English meaning for “reference”;这是“参考”的常见英文意思; if a newspaper article says “Judge Matthews ordered that…,” that is a reference to Judge Matthews.如果一篇报纸文章说“马修斯法官下令……”,那就是对马修斯法官的引用。

When an object is passed by giving the called function a pointer to the object rather than by giving the value of the object, that is a call by reference.当 object 通过给被调用的 function 一个指向 object 的指针而不是通过给 ZA8CFDE6331BD54B6666Z 的值给出调用来传递时,68911BD54B666AC9

C++ created a new feature and called it a “reference,” giving a new meaning to that term. C++ 创建了一个新功能并将其称为“参考”,为该术语赋予了新的含义。 The term has that meaning in the context of C++.该术语在 C++ 的上下文中具有该含义。 It does not have that meaning in medicine, common English, archeology, C, or other contexts.它在医学、普通英语、考古学、C 或其他上下文中没有这个含义。 In C, when we say something is passed by reference, we mean the value of a pointer to it is passed.在 C 中,当我们说通过引用传递某些东西时,我们的意思是传递了指向它的指针的值。

I have always read that C does not have "pass by reference" because, unlike Cpp, pass by reference can only be emulated by passing by value a pointer towards some object — and we should rather say "pass by pointer".我一直读到 C 没有“按引用传递”,因为与 Cpp 不同,按引用传递只能通过按值传递指向某个 object 的指针来模拟——我们宁愿说“按指针传递”。

You're conflating two similar but different meanings of reference .您将reference的两个相似但不同的含义混为一谈。 The phrase "pass by reference" doesn't necessarily pertain to C++'s reference types, which under the hood are essentially pointers with safer, more limited semantics.短语“按引用传递”不一定与 C++ 的引用类型有关,它在本质上是具有更安全、更有限语义的指针。 It means that instead of providing a value directly, you provide it by saying where it is.这意味着您不是直接提供值,而是通过说出它的位置来提供它。 If I want you to paint a house, I can either take you to the house and tell you to paint it, or I can give you the address of the house;如果我要你粉刷房子,我可以带你去房子让你刷,或者我可以给你房子的地址; the address is an object that refers to another object, ie a reference.地址是一个 object,它引用另一个 object,即引用。

Passing a pointer to an object to a function so that the original object may be modified has long been known as passing by reference in languages like C and Pascal, and that's likely why C++ reference types are so named. Passing a pointer to an object to a function so that the original object may be modified has long been known as passing by reference in languages like C and Pascal, and that's likely why C++ reference types are so named. It's a general concept that applies to most languages.这是适用于大多数语言的一般概念。 Passing by pointer seems to be a phrase that came into use after C++ introduced references as distinct types.通过指针传递似乎是在 C++ 将引用作为不同类型引入之后开始使用的短语。 In short, using pass by reference can be ambiguous in a mixed C/C++ context, so using pass by pointer is more specific when that's what you mean.简而言之,在混合 C/C++ 上下文中使用按引用传递可能会模棱两可,因此当您的意思是这样时,使用按指针传递更具体。 But you should understand pass by reference in a language-agnostic context to mean any sort of reference, whether it's a specific type provided by the language or a more general pointer.但是您应该理解 language-agnostic 上下文中的引用传递意味着任何类型的引用,无论是语言提供的特定类型还是更通用的指针。 And when talking about C specifically, pass by pointer and pass by reference mean the same thing.而在具体谈论 C 时,按指针传递和按引用传递是同一个意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM