# Function pointers questions

This topic is 4552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a FSM type of code in which I am trying to avoid the second call. For example I am calling (*CallAI)(Idle). What I really want to do is just do CallAI = &Idle and then somehow force the function to run. Is this possible ? For eg:- If you see in the main function I have the line CallAI = &Idle;. I want to have only a line like this and somehow force the Idle function to execute. Is this possible.
[Source]
void AIFsm(char ch)
{
switch(ch)
{
case 'i' :
CallAI = &Idle;
(*CallAI)(Idle);
break;

case 'w' :
CallAI = &Wander;
(*CallAI)(Wander);
break;

case 'f' :
CallAI = &Flee;
(*CallAI)(Flee);
break;

default :
break;
}

}

int main()
{
char ch;

CallAI = &Idle; /* Want to do something like this and make sure Idle
function gets called */

while(1)
{
ch = fgetc(stdin);
if(ch == 'q')
{
break;
}
else
{
AIFsm(ch);
}
}

return 0;
}

[/Source]
Thanks

##### Share on other sites
Ummm, I don't get it. What will you benefit from having one less line of code ?. You might have misunderstood the purpose of function pointer. Can you state what exactly you're trying to do ?

##### Share on other sites
Hi,

Maybe I might have misunderstood it. I am trying to eleminate the following line
(*CallAI)(Idle);

The reason is because if I have to call that code then I could just call the function itself.

##### Share on other sites
There is no reason to change it really, just leave it as it is.

Dave

##### Share on other sites
I still don't get it. You don't seem to use "CallAI" anyways, so why have it there in the first place?
typedef void ( *ai_function )( void );void AIFsm(char ch, ai_function default_func){        ai_function ai_func = default_func;        switch(ch)        {                case 'i' :                        ai_func = Idle;                        break;                case 'w' :                        ai_func = Wander;                        break;                case 'f' :                        ai_func = Flee;                        break;                default :                        break;        }        ai_func( );}int main(){        char ch;        while(1)        {                ch = fgetc(stdin);                if(ch == 'q')                {                        break;                }                else                {                        AIFsm(ch, Idle);                }        }        return 0;}

The above code does pretty much the same as yours, while the use of "ai_func" is still somewhat superfluous. Instead of a switch statement you could use a look-up table and simply register your AI functions with that. The input character would then be replaced (semantically, at least) by an event or input signal that triggers the registered function.

##### Share on other sites
CallAI = &Idle;(*CallAI)(Idle);

These lines (and some below) I found quite confusing, what are the declarations (the syntax) of Idle () and CallAI () ?.

You may refer to this site for function pointers: www.newty.de

In short, this is the general use of function pointers:
Implemented with switch:
switch (Player.getState ()){case Moving:    Player.Move () ;    break ;case Standing:    Player.JugglingWaitingForUser () ;    break ;case Dead:    Player.DoNothing () ;    break ;}

Implemented with fp:
//This line will replace all the switch above//Player.DoIt () ;(Player.*(Player.m_pfnFunc)) () ;

Player::m_pfnFunc is the function pointer that may point to Player::Move (),
Player::JugglingWaitingForUser () or Player::DoNothing () and be called through Player.m_pfnFunc.

I noticed you used normal function pointer, but that shouldn't be much differences, also, there're some better alternatives that I'm quite sure will be pointed out by the other folks :). Good luck.

• 23
• 15
• 23
• 10
• 19