Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading stored function pointer

Status
Not open for further replies.

gtok

Technical User
Jul 13, 2001
7
0
0
CA
I need help on how to read and reset a value which points to a function here is an example on what i need


//this is what i have now that sets up a global variable
void setfunctionaddress(void (*myfuncaddress)(void))
{
workarea[selected]->data = myfunctionaddress
}

void firstfunction(void)
{
do something
}

void secondfunction(void)
{
do something new
}

//start of example
//#1 i need to setup setfunctionaddress
//#2 need to be able to read/save address what is stored
//#3 need to set setfunctionaddress to new value
//#4 neet to reset setfunctonaddress back from read/save
main()
{
setfunctionaddress(firstfunction);
jumptofunction();
}
void jumptofunction(void)
{
//this is what i need to do
//READ what is stored at setfunctionaddress
setfunctionaddress(secondfunction);
do something
//RESET setfunctionaddress to what was READ
return;
}
 
fried,

i have used your template , now you may manipulate
this program the way you like to achieve what you desire


#include <stdio.h>

typedef void (*funcPtr)();

funcPtr lpFunc[10]={0};//assuming 10 function ptr stored
int currentNo=0;

void setfunctionaddress(/* funcPtr */ void (*myfunctionaddress)() )
{
if(currentNo<10)
lpFunc[currentNo++]= myfunctionaddress;
else
printf(&quot;no more addresses can be accomodated\n&quot;);
}

void firstfunction(void)
{
printf(&quot;am in the first function\n&quot;);
}

void secondfunction(void)
{
printf(&quot;am in the second function\n&quot;);
}


//start of example
//#1 i need to setup setfunctionaddress
//#2 need to be able to read/save address what is stored
//#3 need to set setfunctionaddress to new value
//#4 neet to reset setfunctonaddress back from read/save

void jumptofunction(void)
{
//this is what i need to do
//READ what is stored at setfunctionaddress
int x=0;
while(x<currentNo)
{
(lpFunc[x++])();
}



// do something
//RESET setfunctionaddress to what was READ
// return;
}
void main()
{
setfunctionaddress(firstfunction);
setfunctionaddress(secondfunction);

jumptofunction();
}

hope this helps

bye
yogesh
 
yogesh,

in this way is it possible to simulate run time polymorphism as we have in cpp

manohar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top