|
|
View previous topic :: View next topic |
Author |
Message |
arga
Joined: 09 Sep 2003 Posts: 22
|
Are pointers to functions supported in Ver. 3.173? |
Posted: Sun Feb 15, 2004 11:06 pm |
|
|
//sample basic code
int process1();
int process2();
int process3();
//create a table of pointers to functions
int (*pFunc)() processFunc[] = { process1, process2, process3 }; //compiler error message here: "Function definition different from previous definition"
main()
{
while(1)
{
//so..so..so
//just assume y is declared and assigned a value
x = processFunc[y]();
}
}
process1()
{
return(1);
}
process2()
{
return(2);
}
process3()
{
return(3);
} |
|
|
Ttelmah Guest
|
Re: Are pointers to functions supported in Ver. 3.173? |
Posted: Mon Feb 16, 2004 5:00 am |
|
|
arga wrote: | //sample basic code
int process1();
int process2();
int process3();
//create a table of pointers to functions
int (*pFunc)() processFunc[] = { process1, process2, process3 }; //compiler error message here: "Function definition different from previous definition"
main()
{
while(1)
{
//so..so..so
//just assume y is declared and assigned a value
x = processFunc[y]();
}
}
process1()
{
return(1);
}
process2()
{
return(2);
}
process3()
{
return(3);
} |
The answer is 'yes, but'...
There are significant restrictions to what can/can't be done. Also the ability is there, but not fully 'supported' yet.
Your declaration of the pointer is incorrect to begin with. You are declaring 'pFunc' to be a pointer to a function, and then the declaration gets confused with the array.
The support, does not appear to spread to initialising as part of the declaration, unless the initialisation is to a constant value. Hence if you use #ORG to put a routine at a partucular location you can declare with this address, but not with a normal function name.
The support seems to only accept 'fixed' array indices, not variables.
So:
Code: |
//sample basic code
int process1(void);
int process2(void);
int process3(void);
//create a table of pointers to functions
typedef int (*pFunc)(void);
pFunc processFunc[3];
//This works.
main()
{
int x,y;
processFunc[0]=process1;
processFunc[1]=process2;
processFunc[2]=process3;
//This works
while(1)
{
//so..so..so
//just assume y is declared and assigned a value
x = (*(processFunc[0]))();
//Note that your calling syntax was wrong.
//This works.
x = (*(processFunc[y]))();
//But this doesn't... :-(
}
}
|
Best Wishes |
|
|
|
|
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
|