Pass by reference in C99

人盡茶涼 提交于 2020-05-14 19:28:25

问题


I just read this:

In C++ (and C99), we can pass by reference, which offers the same performance as a pointer-pass.

So I tried this simple code:

#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);
}

The output was:

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'

Now I'm confused. Is pass by referenced indeed supported by C99?


回答1:


That page is wrong. There are no "references" in C (even in C99).

In C, when you want to pass "by reference," you fake reference semantics using a pointer.



来源:https://stackoverflow.com/questions/5359677/pass-by-reference-in-c99

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!