Nested Structs

This is a discussion on Nested Structs within the C Programming forums, part of the General Programming Boards category; Hey Guys I'm having some problems with this code Code: #include <stdio.h> #include<string.h> FILE *in_fptr, /* Input File Pointer; points ...

  1. #1
    Registered User
    Join Date
    Sep 2006

    Nested Structs

    Hey Guys I'm having some problems with this code

    #include <stdio.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 */
    #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();
       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
    	int *found;//store value passed by search routine.
    		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.
    					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
    						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.
    					fprintf(out_fptr,"%s\t\t***Undefined Name***\n", inp_name);
    					fprintf(out_fptr,"%s\n", inp_name);
    				}//end else if.
    				printf("Invalid command...closing.\n");
    				return 0;
    				}//end else.
    			 }//end while.
    	//tbl_flush);//output the resulting data structure to the output
    }/* end of main */
    void init()
    	int i;
    	in_fptr=fopen("", "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.
            if(hash_tbl[*ind_ptr].front == NULL){
                hash_tbl[*ind_ptr].front -> lineno = 5; 
    		else{//an empty index was found, where name should have been.
    	*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
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.
    I support as the first necessary step to a free Europe.

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, 08:14 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Nested structs
    By Sir Andus in forum C Programming
    Replies: 5
    Last Post: 11-28-2006, 09:31 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 01:00 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21