C语言 C 有引用吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4305673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does C have references?
提问by Hymany Hyman
Does C have references? i.e. as in C++ :
C 有引用吗?即在 C++ 中:
void foo(int &i)
回答by Jon Skeet
No, it doesn't. It has pointers, but they're not quite the same thing.
不,它没有。它有指针,但它们并不完全相同。
In particular, all arguments in C are passed by value, rather than pass-by-reference being available as in C++. Of course, you can sort of simulatepass-by-reference via pointers:
特别是,C 中的所有参数都是按值传递的,而不是像在 C++ 中那样按引用传递。当然,您可以通过指针模拟传递引用:
void foo(int *x)
{
*x = 10;
}
...
int y = 0;
foo(&y); // Pass the pointer by value
// The value of y is now 10
For more details about the differences between pointers and references, see this SO question. (And please don't ask me, as I'm not a C or C++ programmer :)
有关指针和引用之间差异的更多详细信息,请参阅此 SO 问题。(请不要问我,因为我不是 C 或 C++ 程序员:)
回答by sbi
Conceptually, C has references, since pointers reference other objects.
从概念上讲,C 有引用,因为指针引用其他对象。
Syntactically, C does not have references as C++ does.
从语法上讲,C 没有 C++ 那样的引用。

