CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Array of functions? Avoiding if's for calling routines

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ivaneduardo747



Joined: 22 Jun 2010
Posts: 3

View user's profile Send private message

Array of functions? Avoiding if's for calling routines
PostPosted: Fri Dec 03, 2010 2:11 pm     Reply with quote

Hello, I'm coding a program that receives a value CMD from the UART (along with other things). I want to call functions depending on the value of CMD. Right now I'm using this:
Code:

if (CMD==0) {
    doThis();
}
else if(CMD==1) {
    doThat();
}
else if(CMD==2) {
    blahBlah();
}

which I find very ineffective and hard to code for 256 functions.
Is there any way I could do some kind of array or lookup table where I assign CMD as the pointer and call the instruction?

Thanks
_________________
0x2BE NOR 0X2BE
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 03, 2010 2:38 pm     Reply with quote

Quote:
Is there any way I could do some kind of array or lookup table ?

Array of function pointers:
http://www.ccsinfo.com/forum/viewtopic.php?t=43696
http://www.ccsinfo.com/forum/viewtopic.php?t=37478
This is probably only reliable if you use the PCH compiler (with 18F PICs).
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 6:15 am     Reply with quote

use Switch()....

CMD would be the "case"
...

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group