Review:   the effect of assigning a reference

  • Consider the assignment statement:

      int a = 4, *p;
    
      p = &a; 

  • When the reference variable p contains the reference to a variable a, p points to a:

    Effect:   we can now access the variable a through the reference variable p !!

The de-reference operator * of C

The de-reference operator *:

  1. The de-reference operator * operates on a reference variable
    (I.e.: * is a unary operator)

  2. The de-reference operator * returns the variable at address given by that reference variable

  3. The data type of the result is the data type of the referenced variable

Example:

 int  a, b, *p;  // p can store a reference to an int typed variables  










The de-reference operator * of C

The de-reference operator *:

  1. The de-reference operator * operates on a reference variable
    (I.e.: * is a unary operator)

  2. The de-reference operator * returns the variable at address given by that reference variable

  3. The data type of the result is the data type of the referenced variable

Example:

 int  a, b, *p;  // p can store a reference to an int typed variables  

   p = &a;       // p contains the reference to variable a 








The de-reference operator * of C

The de-reference operator *:

  1. The de-reference operator * operates on a reference variable
    (I.e.: * is a unary operator)

  2. The de-reference operator * returns the variable at address given by that reference variable

  3. The data type of the result is the data type of the referenced variable

Example:

 int  a, b, *p;  // p can store a reference to an int typed variables  

   p = &a;       // p contains the reference to variable a 

  *p = *p + 1;   // is equivalent to: a = a + 1;  
                 // because *p returns the variable a when p = &a





The de-reference operator * of C

The de-reference operator *:

  1. The de-reference operator * operates on a reference variable
    (I.e.: * is a unary operator)

  2. The de-reference operator * returns the variable at address given by that reference variable

  3. The data type of the result is the data type of the referenced variable

Example:

 int  a, b, *p;  // p can store a reference to an int typed variables  

   p = &a;       // p contains the reference to variable a 

  *p = *p + 1;   // is equivalent to: a = a + 1;  
                 // because *p returns the variable a when p = &a

   p = &b;       // Now: p contains the reference to variable b 



The de-reference operator * of C

The de-reference operator *:

  1. The de-reference operator * operates on a reference variable
    (I.e.: * is a unary operator)

  2. The de-reference operator * returns the variable at address given by that reference variable

  3. The data type of the result is the data type of the referenced variable

Example:

 int  a, b, *p;  // p can store a reference to an int typed variables  

   p = &a;       // p contains the reference to variable a 

  *p = *p + 1;   // is equivalent to: a = a + 1;  
                 // because *p returns the variable a when p = &a

   p = &b;       // Now: p contains the reference to variable b 

  *p = *p + 1;   // is equivalent to: b = b + 1;  
                 // because *p returns the variable b when p = &b

Example C program showing the effect of the de-reference operator

Example:

#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a = 4, b = 8, *p;
  
    p = &a;          // Now *p is an alias for a
    *p = *p + 100;   // ≡ a = a + 100

    printf("a = %d, b = %d\n", a, b);

    p = &b;          // Now *p is an alias for b
    *p = *p + 200;   // ≡ b = b + 200

    printf("a = %d, b = %d\n", a, b);
}    

Key:   after the assignment p = &x;, the expression *p is an alias for x

DEMO: demo/C/set2/deref-op2.c

Relationship between the & and * operators

The * operator is the inverse operator for the & operator

 This identity is valid for any variable x:

      *(&x)x  // Their data types are same too ! 

Example:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;

    i = 4;
    printf("i = %d\n", i );

    *(&i) = 9999; // Equiv to: i = 9999;     
    printf("i = %d\n", i ); 
}

DEMO: demo/C/set2/ref-var0.c

The precendence of the & operator and the * operator -- click here

  • The & operator and the * operator has the 2nd highest precendence among all the C operators

    Helpful hint to help you remember:

      • & and * are unary operators

      • Unary operators have very high precedence

  • A table with the operator precedence is at this webpage: click here

  • For programming convenience, you should remember that:

      • The & operator and the * operator have higher precedence than all arithmetic (+. -, *, /, %), logic (&&, ||, !) and assignment (=, +=, -=, ....) operations

The precedence of the operators will help you understand how the expression is evaluated.