Dereferencing in C

   int z;
   int x[3];
   int *ptr;
                  /* meaning making dereferences explicit */
   z = 3;         /* z = 3                                */
   z = z;         /* z = dereference(z)                   */
   ptr = &z;      /* ptr = &z                             */
   z = (int) ptr; /* z = dereference(ptr)                 */
   z = *ptr;      /* z = dereference(dereference(ptr))    */
   x[z] = 3;      /* x[dereference(z)] = 3                */
   foo( z );      /* foo(dereference(z))                  */

Pointer Assignment

Assignment where the required reference is a reference to a pointer variable and the value is itself a reference.

   ptr_2 = &some_record;
   ptr = ptr_2;   /* pointer assignment */
Two interpretations.
  1. dereference rhs in pointer assignment, so ptr now points to the same place as ptr_2
  2. do not dereference rhs in pointer assignment, so ptr now points to ptr_2
C has both! Draw pictures
int foo() {
   int x, a[5], *ptr, *ptr_2, (*fp)();
   
   ptr_2 = &x;
   ptr = ptr_2;     /* ptr = dereference(ptr_2) */
   ptr = a;         /* ptr = ptr_2 */
   fp = foo;        /* fp = foo */

Storage Model

Dynamic vs. Static

Heap vs. Stack



Copyright © 1995. Curtis E. Dyreson. All rights reserved.
http://www.cs.jcu.edu.au/ftp/web/teaching/Subjects/cp2050a/current/foils/sep5/sep5.html