Поскольку C передается по значению, вам нужно передать указатель на указатель на функцию promt_user_input
:
int main(void)
{
double (*fn)(void);
promt_user_input(&fn);
printf("het %lf",fn());
}
void promt_user_input(double (**fn)(void))
{
int coice;
printf("Enter 1 or 2\n");
scanf(" %d", &coice);
switch(coice){
case 1: *fn = print_1; printf("you typed 1\n"); break;
case 2: *fn = print_2; printf("you typed 2\n"); break;
default: printf("INVALID INPUT"); break;
}
}
Вещи станут легче понять, если вы создадите typedef:
typedef double (*func)(void);
void promt_user_input(func *fn);
int main(void)
{
func fn;
promt_user_input(&fn);
printf("het %lf", fn());
}
void promt_user_input(func *fn)
{
// Same as above....
Тогда вы можете избежать указателя на указатель, если функция вернет значение:
typedef double (*func)(void);
func promt_user_input();
int main(void)
{
func fn = promt_user_input();
if (NULL != fn) {
printf("het %lf", fn());
}
}
func promt_user_input()
{
int coice;
printf("Enter 1 or 2\n");
scanf(" %d", &coice);
switch(coice){
case 1:
printf("you typed 1\n");
return print_1;
case 2:
printf("you typed 2\n");
return print_2;
default:
printf("INVALID INPUT");
return NULL;
}
}