What can you do with the result of &var ?

  • Recall one of the features that a data type provides is:

      • The program can define (= create) variables of the data type to store values of that data type

Example: storing references (=addresses) into reference typed variables

 #include <stdio.h>
 int    i = 4, *p; // p stores addresses of int   typed vars
 short  s = 5, *q; // q stores addresses of short typed vars

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




 } 

What can you do with the result of &var ?

  • Recall one of the features that a data type provides is:

      • The program can define (= create) variables of the data type to store values of that data type

Example: storing references (=addresses) into reference typed variables

 #include <stdio.h>
 int    i = 4, *p; // p stores addresses of int   typed vars
 short  s = 5, *q; // q stores addresses of short typed vars

 int main(int argc, char *argv[])
 {
        &i; // this is the address of an  int typed var
        &s; // this is the address of a short typed var 


 } 

What can you do with the result of &var ?

  • Recall one of the features that a data type provides is:

      • The program can define (= create) variables of the data type to store values of that data type

Example: storing references (=addresses) into reference typed variables

 #include <stdio.h>
 int    i = 4, *p; // p stores addresses of int   typed vars
 short  s = 5, *q; // q stores addresses of short typed vars

 int main(int argc, char *argv[])
 {
    p = &i; // stores the address of var i in p
    q = &s; // stores the address of var s in q 


 } 

What can you do with the result of &var ?

  • Recall one of the features that a data type provides is:

      • The program can define (= create) variables of the data type to store values of that data type

Example: DEMO

 #include <stdio.h>
 int    i = 4, *p;
 short  s = 5, *q;

 int main(int argc, char *argv[])
 {
    p = &i; 
    q = &s; 
    printf("address of i = %p\n", p);
    printf("address of s = %p\n", q);
 } 

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

The effect of assigning a reference

  • Consider the assignment statement:

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

  • The following is happening in memory when the program fragment is executed:

     

The effect of assigning a reference

  • Consider the assignment statement:

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

  • The variable definitions will allocate (= create) the variables a and p in memory:

    The memory location of variable a is also initialized with the value 4.

The effect of assigning a reference

  • Consider the assignment statement:

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

  • Suppose the variable a was allocated at memory address 7000:                                                   

    Then:   &a = 7000 !

The effect of assigning a reference

  • Consider the assignment statement:

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

  • The assignment p = &a; will assign the address 7000 (= &a) to the (reference) variable p:

    I.e.:   p contains the address of the variable a.

The effect of assigning a reference

  • Consider the assignment statement:

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

  • Result:   the variable p references (= points) to the variable a:                                                        

    For this reason, a reference variable is also known as a pointer variable

Important note: data types returned by the & operator

  • Recall this very important fact:

      • There are many different reference data types         

  • The different reference data types are conflicting types

These assignments are illegal (compile warning) due to type conflicts:

 int    i = 4, *p; // p must store an addr of an int typed var
 short  s = 5, *q; // q must store an addr of an short var

 int main(int argc, char *argv[])
 {
    p = &s; // Illegal: stores addr of a short type var in p
    q = &i; // Illegal: stores addr of a int type var in q
 } 

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