简体   繁体   English

通过 C 中值的地址传递

[英]Pass by address of value in C

I know the following is an example of pass by reference in C++, input is passed as a reference:我知道以下是 C++ 中的引用传递示例, input作为引用传递:

void add(int &input){
 ++input;
}

I also know pass by reference is not available in C.我也知道 C 中不提供引用传递。 My question is, does the above syntax mean something else in C (ie pass by value or something), or is it meaningless?我的问题是,上面的语法是否意味着 C 中的其他东西(即按值传递或其他东西),还是没有意义? Trying to compile it in C gives this error:尝试在 C 中编译它会出现此错误:

error: parameter name omitted

does the above syntax mean something else in C?上述语法在 C 中是否还有其他含义?

No, it does not.不,不是的。 It's not valid C at all.这根本不是有效的 C。

The & operator means two things in C. &运算符在 C 中意味着两件事。 The binary one is bitwise "and", and the unary is "address of".二进制是按位“与”,一元是“地址”。 You cannot use it in declarations.您不能在声明中使用它。

C++ chose this for reference variable for two reasons. C++ 选择这个作为参考变量有两个原因。 The first is that since it is not valid C, it will not collide with existing C code.首先是因为它是无效的C,它不会与现有的C代码发生冲突。 When C++ came, they focused pretty hard on making C++ backwards compatible with C.当 C++ 出现时,他们非常专注于使 C++ 向后兼容 C。 In later versions of C++, the backwards compability with C is not a very high priority.在 C++ 的更高版本中,与 C 的向后兼容性不是很高的优先级。 To a large degree, this is because C++ was a fork of a pretty early version of C, and since then both languages have evolved somewhat independently.在很大程度上,这是因为 C++ 是 C 的早期版本的一个分支,从那时起,这两种语言都在某种程度上独立发展。 For instance C99 added (but it was removed later) variable length arrays, which were never added to C++.例如 C99 添加(但后来被删除)可变长度 arrays,从未添加到 C++。 Another example is designated initializers.另一个例子是指定的初始化器。

The other reason is that the meaning of the operator is pretty similar.另一个原因是运算符的含义非常相似。 You can interpret it as "instead of forcing the caller to send the address, I will take the address of whatever he is sending".您可以将其解释为“而不是强迫调用者发送地址,我将使用他发送的任何地址”。 They simply just moved the & to the function prototype instead of the function call.他们只是将&移动到 function 原型而不是 function 调用。

And yes, there are a few other differences between pointers and references too是的,指针和引用之间还有一些其他区别

  • A reference must be initialized.必须初始化引用。 (Assigned upon declaration) (根据声明分配)
  • A reference cannot be reassigned to "point" to another object.不能将引用重新分配为“指向”另一个 object。
  • A reference must always "point" at an object.引用必须始终“指向” object。 It cannot be NULL.它不能是 NULL。

There is one danger with references.引用有一个危险。 In C, you can be certain that a function will never change the variables you send as arguments to a function unless you're sending the address to them. In C, you can be certain that a function will never change the variables you send as arguments to a function unless you're sending the address to them. This C code:此 C 代码:

int main(void)
{
    int a = 42;
    foo(a);
    printf("%d\n", a);
}

will ALWAYS print "42", no matter how the function foo is defined.将始终打印“42”,无论 function foo是如何定义的。 Provided that the code compiles and there's no weird undefined behavior.前提是代码可以编译并且没有奇怪的未定义行为。 In C++, you don't have that guarantee.在 C++ 中,您没有该保证。

No, it is simply invalid syntax in C.不,它只是 C 中的无效语法。

That is actually one of the reasons that C++ picked this syntax for the feature: it wouldn't change the meaning of any existing C code.这实际上是 C++ 为该功能选择此语法的原因之一:它不会改变任何现有 C 代码的含义。

While C does not have pass by reference (and the code will produce compile error), you can get something closer by following the rules:虽然 C 没有按引用传递(并且代码会产生编译错误),但您可以通过以下规则获得更接近的东西:

  • In the prototype, replace & with * const (reference cannot be reassigned).在原型中,将&替换为* const (不能重新分配引用)。
  • In the body, replace reference to varname with (*varname)在正文中,用(*varname)替换对varname的引用
  • When calling the method, replace arg with &(arg).调用方法时,将 arg 替换为 &(arg)。
void add (int *const in)
{
   ++(*in) ;      // increment
   (*in) = 5 ;    // assign 
   int x = *in ;  // Copy value
}

does the above syntax mean something else in C (ie pass by value or something), or it's meaningless?上面的语法在 C 中是否意味着其他东西(即按值传递或其他东西),还是没有意义?

It is meaningless.这是没有意义的。 The program is syntactically ill-formed.该程序在语法上是错误的。

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

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