Review: operators

Review on operators:

  1. An operator operates on one or more operands (= values)

  2. An operator returns some result based on its input operands

  3. The result will always have a specific data type
    (The data type provides the context on how to use a binary number)

    Note:

      • The result's data type can be different from the operand's data type !!

Example:

  The integer addition operator  +:

    (1) +  operates on  2 int operands  (binary operator) 
    (2) +  returns the sum of its operands
    (3) +  returns a result of the type int

     3 + 4 = 7  (result) 

The reference operator & of C

The reference operator &:

  1. The reference operator & operates on one variable of any data type
    (I.e. & is a unary operator)

  2. The reference operator & returns the (base) address of the variable

  3. The data type of the result is the reference type to the data type of the variable

Example:

 int a;
 float b;
 
 &a   returns the address of the variable a
      The data type of the result is reference to an int typed variable

 &b   returns the address of the variable b
      The data type of the result is reference to an float typed variable

Note: the data type of &a and &b are different !!!

A concrete example of the reference operator &

  • Suppose the C program allocates memory for the variable a in memory location 5000:

    Then:

      &a    will return the value 5000
      The data type of the value 5000 is: reference to int (= int *) 

The reference operator & of C - Examples
 

In C, the programmer can use the & operator to find the address of any variable in the program:

  #include <stdio.h>

  int    i = 4;
  short  s = 5;
  float  f = 6.0;
  double d = 7.0;

  int main(int argc, char *argv[])
  {
     printf("Address of i = %p\n", &i );  // %p = pointer 
     printf("Address of s = %p\n", &s );  // The address value
     printf("Address of f = %p\n", &f );  // is printed in hex
     printf("Address of d = %p\n", &d );
  } 

This feature is not available in any other programming language (except C++)

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