-3

float Aco(char** c, int b, char* a) { ...... } float Ma(char** c, int b, char* a) { ...... }

float(*pointer)(char** c, int b, char* a);

?????Funk(int size)
{
 switch (startingLetter)
    {
    case 'a':
        return(&Aco);
        break;
    case 'b':
        return(&Ma);
        break;
    default:
        return NULL;
        break;
   }
}

If I want "Funk" to return NULL or return the pointer to function Ma/Aco what do I need to write instead of ????? ?

or vo
  • 11
  • 3

2 Answers2

1

This should help:

#include <stdio.h>

typedef double (*div_type)(int, int);

double div(int a, int b) {
    return ((double)a / (double)b);
}
div_type div_fun() {
    return (div);
    // or: return (NULL);
}

int main () {
    div_type fun_ptr = div_fun();
    if (fun_ptr != NULL) {
      printf("%f\n", fun_ptr(4, 2));
    } else {
      puts("Undefined");
    }

    return (0);
}

This will produce

% ./a.out
2.000000
Jeremy
  • 566
  • 1
  • 6
  • 25
0

sans typedefs:

float (*Funk(int size))(char** c, int b, char* a)

which reads as

        Funk                                         -- Funk
        Funk(        )                               -- is a function 
        Funk(int size)                               -- with parameter size
       *Funk(int size)                               -- returning a pointer
      (*Funk(int size))(                        )    -- to a function
      (*Funk(int size))(char **c, int b, char *a)    -- with parameters a,b, and c
float (*Funk(int size))(char **c, int b, char *a)    -- returning float

Note that you don't need the & when returning the functions; function expressions, like array expressions, "decay" to a pointer type. So, you can just write:

case 'a':
    return Aco;
    break;
case 'b':
    return Ma;
    break;
default:
    return NULL;
    break;
John Bode
  • 119,563
  • 19
  • 122
  • 198