Skip to main content
GameDev.net gamedev.net
🔒 Locked

function pointers

Started by InFerN0 Aug 27, 2000 at 8:08 PM 2 replies 400+ views
Original Post
InFerN0
InFerN0
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.
InFerN0Not all who wander are lost...
NuffSaid
NuffSaid
    
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.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
An Irritable Gent
An Irritable Gent
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
aig
An Irritable Gent
An Irritable Gent
Whoops, replied to myself rather than edited...

Edited by - An Irritable Gent on August 27, 2000 10:06:29 PM
aig

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.