/***************************************************************************
 *  Columbia University Introduction to Computer Programming in C COMS 1003
 *  Process a string as a pointer
 *
 *  Copyright (C) 2005 Michael E. Locasto
 *
 *  All rights reserved.
 *
 * $Id: pointerstring.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 the use of character
 * pointer variables to refer to multiple strings as logical entities.
 *
 * 
 */
int main(int argc, char* argv[])
{
   /* Create a pointer that points to a character type
    * and set it equal to a string literal.
    */
   char *name = "Michael";

   /* A character array can also be treated as a string,
    * but arrays in general are pointers.
    */
   char myname[] = {'M','I','K','E','\0'};

   char *name_ptr = &myname[0];

   char* data[] = 
   {
      "ant", "apple", "arrow", "zeta", NULL
   };

   /* Create a character pointer, but initialize it to the special 
    * value null.
    */
   char *argv_ptr = NULL;
   
   /* Allocate a counting variable. */
   int i = 0;

   /* Show how to print out a character pointer (a string)*/
   printf("%s\n",name);

   /* argv[] is just an array of character pointers...*/
   while(i<argc)
   {
      printf("argv[%d] = %s\n",i,argv[i]);
      i++;
   }

   while(argc>0)
   {
      argv_ptr = argv[argc-1];
      printf("%s\n",argv_ptr);
      argc--;
   }

   /* Demonstrate treating an array name like a pointer variable */
   while(*name_ptr!='\0')
   {
      printf("%s\n",name_ptr);
      printf("%p\n",name_ptr);
      name_ptr++;
   }

   i = 0;
   while(data[i]!=NULL)
   {
      printf("data[%d] = %s\n",i,data[i]);
      i++;
   }

   printf(" --- \n");

   i = 0;
   while(*data!=NULL)
   {
      printf("data[%d] = %s\n",i,*data);
      (**data)++;
      i++;
   }

   printf("\n");


   exit(0);
}
