C 有引用吗? [英] Does C have references?

查看:19
本文介绍了C 有引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 有引用吗?即在 C++ 中:

Does C have references? i.e. as in C++ :

void foo(int &i)

推荐答案

不,它没有.它有指针,但它们并不完全相同.

No, it doesn't. It has pointers, but they're not quite the same thing.

特别是,C 中的所有参数都是按值传递的,而不是像在 C++ 中那样按引用传递.当然,您可以通过指针模拟传递引用:

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 simulate pass-by-reference via pointers:

void foo(int *x)
{
    *x = 10;
}

...

int y = 0;
foo(&y); // Pass the pointer by value
// The value of y is now 10

有关指针和引用之间差异的更多详细信息,请参阅这个问题.(请不要问我,因为我不是 C 或 C++ 程序员:)

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 :)

这篇关于C 有引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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