-4

I have seen something in this form:

void function( A_struct &var ) {
   var.field0 = 0;
   // ...
}

Since there is & before var, I thought that var is a pointer. But in the body, instead of writing var->field0, var.field0 is written. So, my questions are:

  1. Isn't var a pointer?

  2. What is the difference between writing A_struct &var and A_struct *var in function parameter?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Utku
  • 2,025
  • 22
  • 42

2 Answers2

4

A syntax like A_struct &var is not valid in context of C.

It is used in case of C++ to denote pass-by-reference.

Related: See this question and related answers for more details.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Your question is actually related to C++

void function(A_struct &var) is not valid for C because in C it is used to get an address of a variable. In C++ it is a type of variable which is known as reference. You can see an example of it in here

void function(A_struct *var) is allowed in C and C++, because is a pointer which holds the address of A_struct type variable's address.

Subinoy
  • 478
  • 7
  • 22