Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a function have a reference argument in C?

For example: void foo( int& i ); is not allowed. Is there a reason for this, or was it just not part of the specification? It is my understanding that references are generally implemented as pointers. In C++, is there any functional difference (not syntactic/semantic) between void foo( int* i ) and void foo( int& i )?

like image 865
user478719 Avatar asked Oct 17 '10 19:10

user478719


People also ask

Why is reference not supported in C?

The correct statement is "C does not support implicitly passing a variable by reference" -- you need to explicitly create a reference (with & ) before calling the function and explicitly dereference it (with * ) in the function.

Are arguments passed by reference 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.

Does C allow call by reference?

C and C++ both support call by value as well as call by reference whereas Java doesn't support call by reference.

How arguments are passed to a function using references?

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.


2 Answers

Because references are a C++ feature.

like image 155
dan04 Avatar answered Oct 05 '22 03:10

dan04


References are merely syntactic vinegar for pointers. Their implementation is identical, but they hide the fact that the called function might modify the variable. The only time they actually fill an important role is for making other C++ features possible - operator overloading comes to mind - and depending on your perspective these might also be syntactic vinegar.

like image 20
R.. GitHub STOP HELPING ICE Avatar answered Oct 05 '22 02:10

R.. GitHub STOP HELPING ICE