#include <stdio.h>
#include <string.h>
typedef int (*fx_t)(int); /* fx_t e ponteiro para funcao que recebe e devolve int */
int fx1(int a) { return a + 1; }
int fx2(int a) { return a + 2; }
int fx3(int a) { return a + 3; }
int main(void) {
char y[10];
strcpy(y
, "fx2"); /* obtem y do utilizador */
fx_t x = NULL;
if (strcmp(y
, "fx1") == 0) x
= fx1
; if (strcmp(y
, "fx2") == 0) x
= fx2
; if (strcmp(y
, "fx3") == 0) x
= fx3
; if (x
) printf("%d\n", x
(42)); // chama fx1(42) ou fx2(42) ou fx3(42) */ return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCnR5cGVkZWYgaW50ICgqZnhfdCkoaW50KTsgLyogZnhfdCBlIHBvbnRlaXJvIHBhcmEgZnVuY2FvIHF1ZSByZWNlYmUgZSBkZXZvbHZlIGludCAqLwoKaW50IGZ4MShpbnQgYSkgeyByZXR1cm4gYSArIDE7IH0KaW50IGZ4MihpbnQgYSkgeyByZXR1cm4gYSArIDI7IH0KaW50IGZ4MyhpbnQgYSkgeyByZXR1cm4gYSArIDM7IH0KCmludCBtYWluKHZvaWQpIHsKICAgIGNoYXIgeVsxMF07CiAgICBzdHJjcHkoeSwgImZ4MiIpOyAvKiBvYnRlbSB5IGRvIHV0aWxpemFkb3IgKi8KCiAgICBmeF90IHggPSBOVUxMOwogICAgaWYgKHN0cmNtcCh5LCAiZngxIikgPT0gMCkgeCA9IGZ4MTsKICAgIGlmIChzdHJjbXAoeSwgImZ4MiIpID09IDApIHggPSBmeDI7CiAgICBpZiAoc3RyY21wKHksICJmeDMiKSA9PSAwKSB4ID0gZngzOwogICAgaWYgKHgpIHByaW50ZigiJWRcbiIsIHgoNDIpKTsgLy8gY2hhbWEgZngxKDQyKSBvdSBmeDIoNDIpIG91IGZ4Myg0MikgKi8KICAgIHJldHVybiAwOwp9Cg==