473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the alternative to switch statement

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

Nov 14 '05 #1
2 4398
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.

Nov 14 '05 #2
Emmanuel Delahaye wrote:
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 :
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.

--
Peter

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2272
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative...
10
10896
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the...
35
8315
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
12
3284
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would...
17
2807
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can anybody point me to the innards of it please? Sorry if this is OT on clc and more relevant to comp.compilers. I am confused here too.
43
6859
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
17
2301
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C# and I absolutely *love* it. It makes me think more about what I'm doing it before I just spew code into the editor. I'm writing better code than...
4
1832
by: Roy Gourgi | last post by:
Hi, My program has to be able to call on many classes that differ only by a number and rather than use a switch statement I was wandering if it is possible to do it in C# as it is possible in other languages. For example, if I want to call any one of 100 different classes named clsNo1, clsNo2,clsNo3, ..... clsNo100 from my calling program I...
13
11797
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.