tags:

views:

165

answers:

7

Possible Duplicate:
Passing pointer argument by reference under C?

Are reference variables a C++ concept? Are they available in C? If available in C, why is my code giving a compilation error?

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int a = 10;
  int b = 20;
  int &c = a;
  int &d = b;
  return 0;
}

Output:

bash-3.2$ gcc test.c
test.c: In function `main':
test.c:12: error: parse error before '&' token

Why?

+2  A: 

Pass by reference isn't available in C, use simple pointers instead.

Lazarus
+1  A: 

References are a C++ concept only. In C you are restricted to using only pointers.

Joe M
+5  A: 
  1. It's a concept available on C++ (but not exclusively)
  2. No, it is not available in standard C.
  3. See 1 and 2. See *1 and *2.
Bruno Reis
Should point 3 be "See *1 and *2"? ;)
Lazarus
+5  A: 

You can't use this to declare variables:

int &c = a;

The operator & is used to get the memory address of a variable. So, you could write for example:

int a = 10;
int *c;
c = &a;
Alex
true in C, but C++ allows it.
kenny
+9  A: 

C does not have references as a type. The '&' in C is used to get the address of a variable only. It cannot be used the way you are trying to use it in C.

Matt Davis
+1  A: 

It's been awhile but it looks like you are trying to assign the address of c to the value of a.

Correction: value of a to the address of c. (Thanks Chinmay Kanchi for pointing that out.)

confusedGeek
No, he's trying to assign the value of a to the address of c. Either way, it's downright illegal.
Chinmay Kanchi
A: 

No, reference is C++ only.