/***************************************************************************
 *  Columbia University Introduction to Computer Programming in C COMS 1003
 *  Show basic pointer concepts
 *
 *  Copyright (C) 2005 Michael E. Locasto
 *
 *  All rights reserved.
 *
 * $Id: pointermath.c,v 1.1 2005/11/01 17:45:49 locasto Exp $
 **************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/**
 * The purpose of this program is to demonstrate basic pointer concepts
 * such as maniputaling addresses and the contents of variables.
 *
 * This program also introduces the use of:
 *   the 'pointer-declarator' operator (an asterisk)
 *   the 'dereference' operator (an asterisk)
 *   the 'addressof' operator (an ampersand)
 * 
 */
int main(int argc, char* argv[])
{
   int x = 400;
   unsigned int y = 202;
   float myfloat = 3.14159;
   int i = 0;

   /* A pointer variable holds a forwarding address for another variable */
   int *signpost_ptr = &x;
   /* It can be thought of as a mailbox or signpost. */
   int *mailbox_ptr  = &y;
   /* All types (both primitive and user-defined) can have pointers to them. */
   float *pi_ptr = &myfloat;

   /* We have stored the address of our regular variables into 
    * several 'mailbox' variables. Now we show what is inside those
    * mailboxes. That is, what are the contents of the pointers?
    */
   printf("contents of x is %d\n",x);
   printf("address of x is %p\n",&x);
   printf("contents of signpost_ptr is %p\n",signpost_ptr);
   printf("contents of @[signpost_ptr] is %d\n",*signpost_ptr);
   printf("address  of signpost_ptr is %p\n",&signpost_ptr);

   printf(" --- \n");

   /* What happens if we change the value that a pointer holds? */
   signpost_ptr = signpost_ptr + 1;
   //*signpost_ptr = 5;
   printf("contents of signpost_ptr is %p\n",signpost_ptr);
   printf("contents of @[signpost_ptr] is %d\n",*signpost_ptr);
   printf("address  of signpost_ptr is %p\n",&signpost_ptr);

   printf(" --- \n");

   printf("contents of y is %d\n",y);
   printf("address of y is %p\n",&y);
   printf("contents of mailbox_ptr is %p\n",mailbox_ptr);
   printf("contents of @[mailbox_ptr] is %d\n",*mailbox_ptr);
   printf("address  of mailbox_ptr is %p\n",&mailbox_ptr);

   printf(" --- \n");

   printf("contents of myfloat is %2.5f\n",myfloat);
   printf("address of myfloat is %p\n",&myfloat);
   printf("contents of pi_ptr is %p\n",pi_ptr);
   printf("contents of @[pi_ptr] is %2.5f\n",*pi_ptr);
   printf("address  of pi_ptr is %p\n",&pi_ptr);

   printf(" --- \n");

   /* Note how the value of 'i' changes, but the address of 'i' doesn't. */
   for(i=0;i<10;i++)
   {
      printf("address of i = %p\n",&i);
      printf("value (contents) of i = %d\n",i);
   }



   exit(0);
}
