Hey Guys I'm having some problems with this code
Code:
#include <stdio.h>
#include<string.h>
FILE *in_fptr, /* Input File Pointer; points to the file
containing the input to the program */
*out_fptr, /* Output File Pointer; points to the file to
contain all the output of the program */
*fopen();
#define TRUE 1
#define FALSE 0
#define NAME_LENGTH 10 /* maximum number of characters in name */
#define COM_LENGTH 3 /* number of characters in a command */
#define HT_SIZE 59 /* number of entries in the hash table */
#define STEP_SIZE 29 /* value to be added to the hash value
for resolving hash collisions */
/* an entry in reference queue */
struct rq_entry {
int lineno;
struct rq_entry *next;
};
/* an entry in hash table */
struct ht_entry {
char ht_name[NAME_LENGTH + 1];
struct rq_entry *front; /* a queue is used to keep track of */
struct rq_entry *rear; /* references to a name*/
};
struct ht_entry hash_tbl[HT_SIZE]; /* hash table */
void init();
void search();
int hash();
void tbl_flush();
/********************************************************************/
main()
{
char command[COM_LENGTH + 1], /* the command in the input */
inp_name[NAME_LENGTH + 1]; /* the name in the input */
int line = 1;//keep track of line in input file, for reference queue.
int name_status;//either represents a duplicate entry or undefined
//name.
int *found;//store value passed by search routine.
init();
fprintf(out_fptr,"\tInput Data\n%+2Command %+5Name\n");
while(fscanf(in_fptr, "%s%s", command, inp_name)!=EOF){
line++;//next line in program.
if(strcmp(command, "DEF")==0){//if define is command.
//name needs to be inserted, or
//a duplicate reference note needs to
//be placed.
fprintf(out_fptr,"%+5s\t\t");
name_status=hash(inp_name, TRUE);//passes name and flag telling hash routine
//to not only hash the name, but try to insert
//as well.
if(name_status==FALSE){//name status set to false, when name was already
//defined.
fprintf(out_fptr,"%s\t\t***Duplicate Name***\n", inp_name);
name_status=TRUE;//reset name_status for next name/symbol.
}//end if.
else//name has only been defined once.
fprintf(out_fptr,"%s\n", inp_name);
}//end else.
else if(strcmp(command, "USE")==0){
fprintf(out_fptr,"%5s\t\t", command);
search(inp_name, &found);//name is being used, need to
//check if it exists first.
if(found==FALSE)
fprintf(out_fptr,"%s\t\t***Undefined Name***\n", inp_name);
else
fprintf(out_fptr,"%s\n", inp_name);
}//end else if.
else{
printf("Invalid command...closing.\n");
return 0;
}//end else.
}//end while.
//tbl_flush);//output the resulting data structure to the output
//file.
printf("BYE!\n");
system("PAUSE");
}/* end of main */
/*******************************************************************/
void init()
{
int i;
in_fptr=fopen("lab1.in", "r");
out_fptr=fopen("lab1.out", "w");
for(i=0; i<HT_SIZE; i++){
hash_tbl[i].front=NULL;//initialize each index's queue to empty.
hash_tbl[i].rear = NULL;
strcpy(hash_tbl[i].ht_name, "0");//initialize names in index
//of hash table to empty.
}
}/* end of init */
/********************************************************************/
void search(name_ptr, found_ptr, ind_ptr)
char *name_ptr; /* name to be searched */
int *found_ptr; /* flag to indicate whether or not the name being
searched for is in hash table */
int *ind_ptr; /* index into the hash table */
{
*ind_ptr=hash(name_ptr, FALSE);//set index pointer to index generated by
//hash function.
while(strcmp(hash_tbl[*ind_ptr].ht_name, "0")!=0){//while index is
//not empty.
if(strcmp(hash_tbl[*ind_ptr].ht_name, name_ptr)==0){//if name found.
*found_ptr=TRUE;
if(hash_tbl[*ind_ptr].front == NULL){
hash_tbl[*ind_ptr].front -> lineno = 5;
}
return;
}
else{//an empty index was found, where name should have been.
*ind_ptr=*ind_ptr+STEP_SIZE;
if(*ind_ptr>HT_SIZE)
*ind_ptr=*ind_ptr%HT_SIZE;
}
}
*found_ptr=FALSE;//name not found.
it all compiles just fine but the line hash_tbl[*ind_ptr].front -> lineno = 5; makes it crash at run time and I don't know why, I need to access the lineno variable so I can add things to the queue...any help would be appreciated