Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

InFerN0

function pointers

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a simple scripting engine and I need to be able to call functions dynamicly. The following source shows what I am trying to achieve.
    

int ExecuteCommand(char *command, char *args[16])
{

	void (*funcptr)(void);
	if(FindCommand(head, "quit") == NULL)
		printf("\nCommand Not Found");
	
	funcptr = command;

	funcptr();
	//execute and get return value

	return (1);
}

    
FindCommand checks the list of registered commands to make sure it exists. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
    
funcptr = command;


I don''t think you''ll be able to do this because funcptr is a pointer to a function while command is a character string. You''d need to change the declaration of char *command to void (*command)(void).

This means that your function ExecuteCommand will look like this
int ExecuteCommand(void (*command)(void), char *args[16])
{
//yadda
}

Hope that helped.

Share this post


Link to post
Share on other sites
Build an array, where each element contains a function pointer and a string for the function's name name. Make the last element in the array contain a function pointer that is NULL. Change your "FindCommand" function so that it searches through this array for the name, and returns the function pointer or the NULL if not found. Your code is close, but should be:

    
int ExecuteCommand(char *command, char *args[16])
{
void (*funcptr)(void);

funcptr = FindCommand(command);
if(funcptr == NULL)
printf("\nCommand Not Found");
else
funcptr();
return 1;
}



Edited by - An Irritable Gent on August 27, 2000 10:05:48 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!