What is the alternative to switch statement

M

MJ

Hi
We can use the switch statement, or if else statement instead of switch

One more method is there which can replace the switch using the
function pointer or the array of function pointer. If any one has any
idea how to do it , could guide me

MJ
 
E

Emmanuel Delahaye

MJ wrote on 01/05/05 :
We can use the switch statement, or if else statement instead of switch
Yes.

One more method is there which can replace the switch using the
function pointer or the array of function pointer. If any one has any
idea how to do it , could guide me

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 :


/* Compile unit A */
typedef int F ();

F fa;
F fb;
F fc;

int main (void)
{
F *af[] =
{
fa,
fb,
fc,
};

af[0] (123, "abc");
af[1] (456, "def");
af[2] (789, "ghi");

return 0;
}/* Compile unit B */

int fa(void)
{
}

int fb(int a)
{
}

int fc(int a, char *b)
{
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
P

Peter Nilsson

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.
af[1] (456, "def");

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top