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 2 4375
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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#...
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |