Basic C question

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Basic C question

6,091 Views
ernestsnaith
Contributor I
My application revolves around 10 different scenarios, say Sen1 to Sen 10. If i determine which scenario i have what is the best way to call their corresponding fucntion say Function1 - Function 10. Each of these functions has a large amount of code associated with it and the scenario is constanlty changing.
 
Is there a dictionary for C statements around?

 

Message Edited by ernestsnaith on 05-03-200602:53 AM

Labels (1)
0 Kudos
7 Replies

380 Views
admin
Specialist II

You could also make an array of pointers to your functions, then use the error numbers as an index into the array.

However, this different just for the sake of being different.  I would say that 9 out of 10 people would use a "switch."

 

I would :smileywink:

 

-Tomahawk

0 Kudos

380 Views
ernestsnaith
Contributor I
I appologise for how basic these questions are but am i understanding 'Switch' correctly? I have a number 0-10 in Thno, should it branch to corresponding ToothX?
 
 switch (Thno) {
   case 0: Error(); break;
   case 1: Tooth1(); break;
   case 2: Tooth2(); break;
   case 3: Tooth3(); break;
   case 4: Tooth4(); break;
   case 5: Tooth5(); break;
   case 6: Tooth6(); break;
   case 7: Tooth7(); break;
   case 8: Tooth8(); break;
   case 9: Tooth9(); break;
   case 10: Error(); break;   
 }
 
This is what first put me off switch, I have lots of error such as
implicit parameter declaration for tooth1

Message Edited by ernestsnaith on 05-03-200612:57 PM

0 Kudos

380 Views
bigmac
Specialist III
Hello Earnest,

 ernestsnaith wrote:
I app ologise for how basic these questions are but am i understanding 'Switch' correctly? I have a number 0-10 in Thno, should it branch to corresponding ToothX?
 switch (Thno) {
   case 0: Error(); break;
   case 1: Tooth1(); break;
   case 2: Tooth2(); break;
   case 3: Tooth3(); break;
   case 4: Tooth4(); break;
   case 5: Tooth5(); break;
   case 6: Tooth6(); break;
   case 7: Tooth7(); break;
   case 8: Tooth8(); break;
   case 9: Tooth9(); break;
   case 10: Error(); break;   
 }
 
This is what first put me off switch, I have lots of error such as
implicit parameter declaration for tooth1

Suggest you remove the case 0: and case 10: switch statements, and make the final switch statement -

  default: Error();

as Peg suggested.  This will pick up any value ot Thno other than 1 through 9.

Each function that is called will need to have a prototype declared prior to its first use.  I suspect this may be the reason for your compile errors.  The prototypes might take the form -

void Tooth1(void);
void Tooth2(void);
etc.

Normally these would be part of a header file, and the header file included near the beginning of the main file.

Regards,
Mac

 

 


0 Kudos

380 Views
Sten
Contributor IV

I think Peg did mean something like this:

switch (scenario) {

case 0: func0(); break;

case 1: func1(); break;

...

}

An other way to do it would be using a array of pointers to functions:

void (*apf[10])(void) = {func0, func1, func2...};

(*apf[scenario])();

In both cases you should write the code for the different scenarios in the functions func0..func10.

 

An excellent 'C dictionary' book would be 'C, A Reference Manual' by Samuel P. Harbison and Guy L. Steele Jr.

 

0 Kudos

380 Views
ernestsnaith
Contributor I

Can you place a lot of code within a switch though? Ive only just started with C but i thought it just picked the closest value and returned it.

What i am after is a goto with a parameter if this is possible or wise. Is the following possible at all

goto ("Function" + ScenNo)

 

Message Edited by ernestsnaith on 05-03-200604:22 AM

0 Kudos

380 Views
peg
Senior Contributor IV

Hi,

You can put in as much code as you please, but if you want to keep it readable, normally just a couple of function calls.

e.g.

switch (scenario){

   case scenario1:

       func_scen_1();

       break;

   case scenario2:

       func_scen _2();

       break;

   default:

       panic();    //there is more scenarios than I bargained on!

}

But also remember Rule No. 1

1. Avoid the use of goto!

only use it in phrases like "goto the fridge for a beer" and "goto the beach" etc.

Regards Peg

 

0 Kudos

380 Views
peg
Senior Contributor IV

Hi,

How about "switch".

Regards Peg

 

0 Kudos