/***************************************************************************
 *  Columbia University Introduction to Computer Programming in C COMS 1003
 *  Show how to use simple function pointers
 *
 *  Copyright (C) 2005 Michael E. Locasto
 *
 *  All rights reserved.
 *
 * $Id: functionptr.c,v 1.1 2005/11/15 00:49:33 locasto Exp $
 **************************************************************************/

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

#define  YOU   5000
#define  ME    1000

int (*hello)(float);

int my_hi(float f)
{
   printf("hi\n");
   return ME;
}

int your_hello(float f)
{
   return YOU;
}

/**
 * Show function pointers
 */
int main(int argc, char* argv[])
{
   int (*hello_function)(float f) = NULL;
   int result = 0;

   if( 0==strncmp(argv[0],"./hi",4))
   {
      hello_function = &my_hi;
   }else if(0==(strncmp(argv[0],"./hello",7))){
      hello_function = &your_hello;
   }else{
      fprintf(stderr,"bad program name [%s], name it 'hi' or 'hello'\n",
              argv[0]);
      exit(-1);
   }

   /* call the function via the pointer to it. */
   result = hello_function(5.6);

   printf("result = %d\n", result);

   exit(0);
}
