开发者

Does C have references?

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

void开发者_C百科 foo(int &i)


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

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


Conceptually, C has references, since pointers reference other objects.

Syntactically, C does not have references as C++ does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜