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

Are pointers to functions supported in Ver. 3.173?

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



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Are pointers to functions supported in Ver. 3.173?
PostPosted: Sun Feb 15, 2004 11:06 pm     Reply with quote

//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?
PostPosted: Mon Feb 16, 2004 5:00 am     Reply with quote

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
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