Thread: Nested Structs

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    1

    Nested Structs

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why in the world are you using K&R style C? From the looks of it, the error checking that ISO C does for you with prototypes will easily solve your problem.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One question - how old is your compiler or C book?

    Your style is obsolete by at least 15 years.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested structs
    By bandal27 in forum C Programming
    Replies: 26
    Last Post: 12-13-2008, 09:14 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Nested structs
    By Sir Andus in forum C Programming
    Replies: 5
    Last Post: 11-28-2006, 10:31 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM