/* L-26 MCS 572 Wednesday 15 March 2006 : illustration of get_next.
 *
 * This program is a cartoon of an application waiting for user input
 * to produce the next items.  These items could be picture files and
 * the computational intensive application could be image decompressing.
 *
 * If the name of this program is "get_next", run it typing
 * 
 *   get_next 7 2 4
 *
 * at the command prompt.  */

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

void *get_next ( void *n );
/* 
 * DESCRIPTION :
 *   Returns a buffer with the next n elements. */

int main ( int argc, char *argv[] )
{
   int i,j,n;
   int *buffer;
   char ans;

   for(i=1; i<argc; i++)
   {
      n = atoi(argv[i]);

      buffer = get_next((void*)&n);

      printf("buffer : ");
      for(j=0; j<n; j++)
         printf(" %d",buffer[j]);
      printf("\n");

      printf("press enter to continue");
      scanf("%c",&ans);  /* skip end of line */
      free(buffer);
   }
   return 0;
}

void *get_next ( void *n )
{
   static int nb = 0;
   int i,*b,*k;

   k = (int*)n;

   b = (int*)calloc(*k,sizeof(int));

   for(i=0; i<*k; i++) b[i] = ++nb;

   return b;
}
