Page 1 of 1

PostPosted: Tue Oct 07, 2003 8:07 pm
by deegee

Can anyone tell me how to call a procedure from the "main" procedure in PocketC, whose name consists of a variable eg I want to call one of the procedures << sub/x >> where x is a variable integer for example and I have created procedures named << sub/1 >>, << sub/2 >> etc.

Many thanks


PostPosted: Wed Oct 08, 2003 12:59 am
by cable_guy_67
How about just switching to the proper call?

Code: Select all
    case 0:
    case 1:
    case 2:
        alert("No matching function.");

Unless you have a hundreds of functions wouldn't this work out?

PostPosted: Thu Oct 09, 2003 9:54 am
by deegee
Thanks Cable Guy. Trying it now.

PostPosted: Thu Oct 09, 2003 2:01 pm
by jstadolnik
A faster way is to use function pointers. For example:

int func1(int a) { return(a+1); }
int func2(int a) { return(a+2); }
int func3(int a) { return(a+3); }

main() {
pointer pFunc;
int i,j;
int x=1; //selection variable

//assign function pointer based on value of x
switch(x) {
case 0: pFunc=func1; break;
case 1: pFunc=func2; break;
case 2: pFunc=func3; break;

//now use the pointer
for(i=0;i<1000;i++) {
j=(*pFunc)(j); //call the selected function

The use of function pointers is very fast. Be careful though, PocketC doesn't do automatic type conversion on variables passed to a function called through a pointer. Use casting if you need to ensure that a variable is of the correct type (e.g. j=(*pFunc)((int)j); ). Definitely don't cast if the variable is already of the correct type because casting slows things down.


The PToolboxLib guy.