Emmanuel said:
MJ wrote on 01/05/05 : switch
If you have some action numbered from 0 to N-1, you can have an array
of N pointers to functions. It's fast. Just be sure that the list of
parameters is meaningful... Nota that you can call a function with
parameters and that you don't have tu use them...
For example, this is valid :
No, it isn't.
/* Compile unit A */
typedef int F ();
This simply tells the compiler that F is a function with unspecified
parameters. But the arguments you supply when calling such a function
must still match the parameters of function definition in question.
F fa;
F fb;
F fc;
int main (void)
{
F *af[] =
{
fa,
fb,
fc,
};
af[0] (123, "abc");
This is invalid since the arguments supplied must match the parameters
in the definition of the called function.
Ditto.
The only thing the non-prototype () effectively does is turn off some
of the otherwise required diagnostics.
Note that this use of () is obsolete. [But will probably remain in the
language for all future standards.]
af[2] (789, "ghi");
return 0;
}/* Compile unit B */
int fa(void)
{
}
int fb(int a)
{
}
int fc(int a, char *b)
{
}
If you're not sure why, just ask youself, in the code below, why should
[1] be valid (it isn't), but [2] require a diagnostic?
int foo();
int bah(int);
int main()
{
foo(42, 42);
bah(42, 42);
}
Even if you declared...
int fd(int a, ...);
....in the earlier code you would still have problems as you can only
call variadic functions which have been prototyped, lest you invoke
undefined behaviour.