djfish's studio

4/12/2009

pointers to functions-cstrip.c

/*pointers to functions*/
#include <stdio.h>
struct table_entry
{
       void (*prtptr)(char c);    /* pointer to a print function */
       int next_state;      /* state to go to next */
};
/*
** Strip c comments from standard input and
* send striipper code to standard output.
*/

main()
{
      FILE *file;
      file = fopen("cstrip.c","r");
      int c,col, row;
      void charprt(), noprt(), slsh();     /* print functions */
      static struct table_entry table[][3]=
      {
             {{noprt, 2},{charprt,0},{charprt,0}},
             {{noprt, 1},{noprt,3},{noprt,1}},
             {{slsh, 0},{noprt,1},{slsh,0}},
             {{noprt, 0},{noprt,1},{noprt,1}}
      };
      row=0;
      while ((c=getc(file))!=EOF)       /* read till end of file*/
      {
            /* find column matching input character*/
            if(c=='/')
                col=0;
            else if(c=='*')
                col=1;
            else
                col=2;
            (*table[row][col].prtptr)(c); /* execute approp print function*/
            row=table[row][col].next_state;  /* get next state*/
      }
      fclose(file);
}

void noprt(c)            /*print nothing*/
char c;
{}

void charprt(c)            /*print a char*/
char c;
{
     putchar(c);
}

void slsh(c)              /*print a slash, then a char */
char c;
{
     charprt('/');
     putchar(c);
}

No comments:

Post a Comment