Re: function pointers
- From: Michal Nazarewicz <mina86@xxxxxxx>
- Date: Sun, 06 Jan 2008 19:40:54 +0100
hifrnds007@xxxxxxxxx wrote in comp.lang.c:
how the function poiners avoids the usage of switch cases?
"Tomás Ó hÉilidhe" <toe@xxxxxxxxxxx> writes:
Here's a typical switch statement:
switch (user_input)
{
case 0: DeleteFile(); break;
case 1: CopyFile(); break;
case 2: RenameFile(); break;
case 4: MoveFile();
}
And here's the function pointer equivalent:
void (*const funcs[4])(void) = {DeleteFile,CopyFile,Renamefile,MoveFile};
funcs[user_input];
Forgot about "()" and to be precise you should check if user_input is >=
0 and <= 4.
Whether that is faster is another matter. Compiler may implement
a switch using an array of locations in program and achieve the same or
even higher speed (no need to call a function, simple jump).
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
.
- Follow-Ups:
- Re: function pointers
- From: Tomás Ó hÉilidhe
- Re: function pointers
- References:
- function pointers
- From: hifrnds007
- Re: function pointers
- From: Tomás Ó hÉilidhe
- function pointers
- Prev by Date: Re: smuggling data in and out of alarm handlers and the like
- Next by Date: Re: Lazy evaluation question
- Previous by thread: Re: function pointers
- Next by thread: Re: function pointers
- Index(es):