在C99中通过引用传递

ant*_*a40 4 c99 pass-by-reference

我刚才读这个

在C ++(和C99)中,我们可以按引用传递,它提供与指针传递相同的性能。

因此,我尝试了以下简单代码:

#include <stdio.h>

void blabla(int& x){
    x = 5;
}

int main(){
    int y = 3;
    printf("y = %d\n", y);
    blabla(y);
    printf("y = %d\n", y);
}
Run Code Online (Sandbox Code Playgroud)

输出为:

gcc test.c -o test -std=c99
test.c:3:16: error: expected ';', ',' or ')' before '&' token
test.c: In function 'main': 
test.c:10:2: warning: implicit declaration of function 'blabla'
Run Code Online (Sandbox Code Playgroud)

现在我很困惑。C99确实支持引用传递吗?

Jam*_*lis 5

该页面是错误的。C中没有“引用”(即使在C99中)。

在C语言中,当您要传递“按引用”时,您将使用指针来伪造引用语义。

  • @ anta40:不。语言规范没有列出它可能具有但没有的所有可能功能。C没有引用,因为它们没有出现在规范中的任何地方。 (7认同)
  • @Mehrdad:我更担心该页面的作者[写过一本书](http://www.amazon.com/Introduction-Design-Patterns-Qt/dp/0131879057/ref=ntt_at_ep_dpt_1)。 (3认同)