K
Einen hab ich noch, einen hab ich noch...
#include <conio.h>
#include <stdio.h>
#include <string.h>
/* Structures and Functions */
typedef int (tfunc)(int i);
struct tstrfunc
{ char *pcstr;
tfunc *pfunc;
};
tfunc *a_str_switch(struct tstrfunc *psf, char *pcstr)
{
int i;
for (i = 0; psf[i].pcstr; i++)
if (strcmp(psf[i].pcstr, pcstr) == 0)
break;
return psf[i].pfunc; /* always return a valid pfunc */
}
/* actual functions */
static int ffirst (int i) { printf("first %i\n", i); return 1; }
static int fsecond (int i) { printf("second %i\n", i); return 2; }
static int fdefault(int i) { printf("default %i\n", i); return 3; }
int main()
{
/* actual data */
static struct tstrfunc pstrfunc[] =
{ { "first", ffirst },
{ "second", fsecond },
{ 0, fdefault } /* Mark End as { 0 and default func } */
};
/* - - - - - - - - - - - - - - - - - - */
a_str_switch(pstrfunc, "second")(43331);
/* - - - - - - - - - - - - - - - - - - */
getch();
return 0;
}