Jump to content
  • Advertisement

Archived

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

InFerN0

problem

This topic is 6532 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

this code compiles fine but doesn''t work
    
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>



typedef struct 
{
	char * token;
}token_t;
 
typedef struct 
{

	char *command;

	char *args[16];
	int numargs;
}command_t;

static token_t   tokenlist[256];
static int       numtokens      = 0;
char seps[]   = " \t\n";
command_t command[32];
int curcommand = 0;


void GetInput(char * buffer)
{
	

	gets(buffer); 
	
	
}


void ProcessInput(char * buffer)
{
	getch();                                
	char * token = NULL; 
    int curarg = 0;                         
    token = strtok(buffer, seps);
    getch();
	while(NULL != token) 
    {

		tokenlist[numtokens].token = strdup(token);
                                     
        numtokens++;
                                          
        
                                       
		if(strchr(tokenlist[numtokens].token,''-''))
		{
			command[curcommand].command = tokenlist[numtokens].token;
			curcommand++;
			
		}
		else
		{
			command[curcommand].args[curarg]= tokenlist[numtokens].token;
			command[curcommand].numargs++;
			curarg++;
		}
		token = strtok(NULL, seps);
	}
	getch();
                                         
}
       
void displaycommand(int icommand)
{
	printf("\n%s", command[icommand].command);
}


void displaytokenlist()
{
	int y;
	
	for(y = 0; y < numtokens; y++)
	{

		printf("\n%s",tokenlist[y].token);
		
	
	
	}
}



void freetokenlist()
{
	int y;
	
	for(y = 0; y < numtokens; y++)
	{

		if(NULL != tokenlist[y].token)
		{

			free(tokenlist[y].token);
		
		}
	
	}
}

void main( void )
{
	char * string;
	GetInput(string);

	ProcessInput(string);

	displaytokenlist();
	displaycommand(0);	
	

	freetokenlist();
}
                                          
    
thanks.

Share this post


Link to post
Share on other sites
Advertisement
At a quick glance, the first thing I can see is that you are passing a character pointer to GetInput that doesn''t have any memory allocated for it. This will make gets() write into a bad memory address and probably cause a GPF.

Try char string[100] (or whatever) instead. Or allocate the memory dynamically.

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!