Я не могу найти в стандарте что-либо, связанное с общими указателями функций, в часто задаваемых вопросах C (Вопрос 1.22) они используют:
typedef int (*funcptr)(); /* generic function pointer */
typedef funcptr (*ptrfuncptr)(); /* ptr to fcn returning g.f.p. */
Немного поиграв с конечными автоматами, это мой подход:
#include <stdio.h>
#define STM(x) (stm)x
typedef void (*stm)(void);
typedef stm (*pstm)(void *);
stm start(void *),
state1(void *),
state2(void *),
state3(void *),
stop(void *);
static int exit_state(int state)
{
char str[2];
int c;
printf("Exit state %d? ", state);
if (fgets(str, sizeof str, stdin)) {
while (((c = fgetc(stdin)) != '\n') && (c != EOF));
return (str[0] == 'y') || (str[0] == 'Y');
}
return 0;
}
static void state_machine(pstm pstart, void *data)
{
pstm state = pstart;
while (state != NULL) {
state = (pstm)(*state)(data);
}
}
stm start(void *data)
{
puts("Starting state machine");
*(char **)data = "Comes from start";
return STM(state1);
}
stm state1(void *data)
{
puts(*(char **)data);
puts("State 1");
if (!exit_state(1)) {
return STM(state1);
}
*(char **)data = "Comes from state 1";
return STM(state2);
}
stm state2(void *data)
{
puts(*(char **)data);
puts("State 2");
if (!exit_state(2)) {
return STM(state2);
}
*(char **)data = "Comes from state 2";
return STM(state3);
}
stm state3(void *data)
{
puts(*(char **)data);
puts("State 3");
if (!exit_state(3)) {
return STM(state1);
}
return STM(stop);
}
stm stop(void *data)
{
(void)data;
puts("Stopping state machine");
return NULL;
}
int main(void)
{
char *data;
state_machine(start, &data);
return 0;
}
Мой вопрос: допустимо использовать
typedef void (*stm)(void);
как общий указатель на функцию?
из того, что я вижу, кажется, что мы можем использовать любой тип прототипа перед созданием слепка, т.е.
typedef long double (*stm)(unsigned long long);
также действует
мои предположения верны?





Цитата из: http://c-faq.com/ptrs/generic.html
It is guaranteed, however, that all function pointers can be interconverted, as long as they are converted back to an appropriate type before calling. Therefore, you can pick any function type (usually int ()() or void ()(), that is, pointer to function of unspecified arguments returning int or void) as a generic function pointer. When you need a place to hold object and function pointers interchangeably, the portable solution is to use a union of a void * and a generic function pointer (of whichever type you choose).
Итак, да, мы можем использовать typedef void (*stm)(void);
ИЛИ typedef long double (*stm)(unsigned long long); как универсальный указатель на функцию.
Ссылки на выделенный текст по ссылке:
ИСО гл. 6.1.2.5, разд. 6.2.2.3, разд. 6.3.4 Обоснование гл. 3.2.2.3 охрана труда и техника безопасности 5.3.3 стр. 12
Обновлено: (Добавление подробностей из другого ответа)
Ссылка в проекте n1570 для C11: 6.3 Преобразования / 6.3.2.3 Указатели § 8:
A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.
Я думаю, что компилятор позаботится об этом.