Review + pre-req: what is a data type

A data type in a programming language provides these features to the programmer:

  1. The programmer can define (= create) variables of the data type to store values of the data type

  2. A data type also provide specialized operations on the values of the data type

Example:

 Data type int:  represent whole values

 (1) Defining variables:   int x, y, z;

 (2) Specialized operations for int:  +, -, *, /, % 

                      x = x + y;

Prelude to the reference data type - review: what is a reference
 

  • Reference = an address in memory (a.k.a. a (memory) pointer)

    The address value 4 "points" to memory location 4 in the memory

  • An address is (binary) integer number !!!

Reference data type
 

  • Reference data type = a data type uses integer numbers as memory addresses

  • The reference data type in C allows C programmers to:

      1. Define variables of a reference type that can store memory addresses

        (I.e.: a reference typed variable stores an integer that is used as a memory addresses

      2. Apply special operations (& and *) that uses memory addresses (= integers) to achive specialized effects

The reference data types in C
 

  • C has many (different) reference data types !!!

  • In fact:

      • Every data type has a corresponding reference data type !!     

  • Examples of reference data types:

        address of int                typed variables
        address of float		typed variables
        address of struct BankAccount typed variables      
        etc
      

    (There are even reference data types to reference data types !!!)

Defining a reference typed variable
 

  • C has an unorthodox way to define reference typed variables

  • Syntax to define a reference variable of the data type dataType is:

        dataType   *varName  ;       
      

    Meaning of this definition:

      • the variable varName stores an address (= reference) of a variable of the data type dataType

Defining a reference typed variable - Example
 

  • Examples:

       int   a, *p;  // a stores an integer value 
                     // p stores an address of 
      	       // an int typed variable  
      
       float b, *q;  // b stores a float value
                     // q stores an address of 
      	       // a float typed variable
      

Note: the symbol * is also used as the multiply operator (e.g.: x * y)

Note: * used as multiply operation is a binary operator
Note: * used as reference operation is an unary operator

Note on the syntax to define reference typed variables
 

  • This definition:

         int *p;         
      

    defines a variable named p --- not one named *p !!!

  • Maybe it's clearer to rewrite the definition like this:

         int* p;         
      

    Also:   p always stores an address of an int typed variable
    (I.e.: p cannot store an address of a different data type)

Operations on address values

  • The most important feature of a data type is:

      • The operations on the values of a data type

  • Next, we will study 2 very unusual operators:

      1. The reference operator   &

      2. The de-reference operator   *             

    These 2 "unusual" operations makes the C programming language a very powerful programming language (that can cause a lot of harm if not used correctly)

  • C is known as a system programming language because the reference data type allows C programmers to perform powerful (and "dangerous") operations