Hi, i have a problem with my code. Currently i am trying to learn function pointers and the best way is practicing but my little program don't work as it should. After i call (*execute) it does not show anything and i don't get what's wrong. execute is assgined to point to a function by pick_function. Maybe could someoane look on to my code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef void (*fptr)(void);
void func1(void)
{
printf("\n\t\t Message 1\n\n");
}
void func2(void)
{
printf("\n\t\t Message 2\n\n");
}
void func3(void)
{
printf("\n\t\t Message 3\n\n");
}
void func4(void)
{
printf("\n\t\t Message 4\n\n");
}
fptr pick_function(fptr array_of_fptr[])
{
int insert;
printf("\n\n\t\t Choose a message between 0-6 \n\n");
do scanf("%d",&insert);
while((insert<0)||(insert>3));
switch (insert)
{
case 0:
return array_of_fptr[0];
break;
case 1:
return array_of_fptr[1];
break;
case 2:
return array_of_fptr[2];
break;
case 3:
return array_of_fptr[3];
break;
}
}
int main()
{
fptr array_of_fp[4]={func1,func2,func3,func4},execute;
execute=pick_function(array_of_fp);
(*execute);
}
Thanks in advance!