Search code examples
cpointersparametersreferencedereference

What is the difference between * and & in function parameters?


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?


Solution

  • 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.