Advertisement

problem

Started by September 01, 2000 11:28 PM
0 comments, last by InFerN0 24 years, 6 months ago
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.
InFerN0Not all who wander are lost...
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.

This topic is closed to new replies.

Advertisement