Background info:   what (exactly) is a data type

  • Data type:

      • A data type defines a domain of values used to represent a certain category of information

  • A data type in a programming language provides the following capabilities:

      1. Allow the definition (= creation) of variables of the data type to store values (= information) of that data type

      2. Provide specialized operations on variables/values of the data type

  • Example: the data type int (represents whole numbers)

      (1) Definition of int typed variables:   int x, y, z;
    
      (2) Operations on int values:            +, - , *, /, % 

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

  • Reference = an integer number which is used as an address of some memory location (a.k.a.: pointer)

    E.g.: the address value 4 "points" to the memory location 4 in the memory

  • Note:

      • An address is always a binary integer number !!!
      • I use decimal numbers for convenience

Reference data type

  • Reference data type:

    • A reference data type = a data type that uses integer numbers as memory addresses

    • I.e.:

      • A reference value is an integer
      • The program will use the integer as an memory addresses

  • Furthermore:   C allows programmers to:

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

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

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

The reference data types in C

  • Important note:

      • C has many different reference data types

  • In fact:

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

  • Examples of reference data types:

        reference to an int  
        reference to an float 
        reference to an struct BankAccount     
        etc                    (they are aa different ref types)
      

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

Defining a reference typed variable

  • Syntax to define a reference variable:

        dataType   *varName  ;       
      

  • Meaning of this definition:

      • Define a reference variable with name varName

      • The variable varName stores a reference (= address) to a variable of the type dataType

  • Example:

       int *p;  // (1) Defines a variable with name p
                // (2) p stores a reference to an int typed variable

Defining a reference typed variable - more example

  • Examples:

       int   a, *p;  // Defines 2 variables: a and p 
                     // a stores an integer value 
                     // p stores the reference (=address) of 
      	       //   an int typed variable  
      
       float b, *q;  // Defines 2 variables: b and q 
                     // b stores a float value
                     // q stores an reference (=address) of 
      	       //   a float typed variable
      


  • Note:

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

  • How can the C compiler (or you) tell the difference:

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

Note on the syntax to define reference typed variables

  • This definition:

         int *p;   // Preferred way         
      

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

  • Maybe it is clearer to write the definition like this:

         int* p;   // Clearer. but not preferred !      
      


  • Very important fact:

      • int * p will always stores an reference (=address) to an int typed variable

        (I.e.: p cannot store a reference to a different data type)

Operations on reference values

  • Recall that:

    • A data type also provide specialized operations on variables/values of that data type

  • Example: operations on int values:

         +      Add 2 integer values
         -      Subtract 2 integer values
        etc
      


  • The reference data type has 2 very powerful (= dangerous) operations:

      1. The reference operator     &

      2. The de-reference operator   *             

  • C is known as a system programming language because C programs can execute these low level (= dangerous) operations