Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by reference in C99

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?

like image 221
anta40 Avatar asked Mar 19 '11 02:03

anta40


People also ask

Is pass by reference possible in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

How do you pass by reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

What is the difference between pass by value and pass by reference in C?

Definition. Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.

Which language is pass by reference?

What Is Pass by Reference? Pass by reference is something that C++ developers use to allow a function to modify a variable without having to create a copy of it. To pass a variable by reference, we have to declare function parameters as references and not normal variables.


1 Answers

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.

like image 160
James McNellis Avatar answered Sep 22 '22 04:09

James McNellis