Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C have references?

Tags:

c

reference

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

void foo(int &i) 
like image 909
jacky jack Avatar asked Nov 29 '10 16:11

jacky jack


People also ask

Does C pass-by-reference?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

How do references work in C?

The main use of references is acting as function formal parameters to support pass-by-reference. In an reference variable is passed into a function, the function works on the original copy (instead of a clone copy in pass-by-value). Changes inside the function are reflected outside the function.

What is a reference in C language?

The C Language Reference describes the C programming language as implemented in Microsoft C. The book's organization is based on the ANSI C standard (sometimes referred to as C89) with additional material on the Microsoft extensions to the ANSI C standard. Organization of the C Language Reference.

Why there is no call by reference in C?

Each programming language has its own terminology. And in C call by reference or pass by reference means pass indirectly through a pointer to an object. This wrong statement arises when somebody is saying about C but is using the terminology of the definition of the term reference introduced in other languages.


2 Answers

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

like image 98
Jon Skeet Avatar answered Oct 04 '22 15:10

Jon Skeet


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

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

like image 22
sbi Avatar answered Oct 04 '22 15:10

sbi