Question

I have to build a hash table data structure for this project, which I have done it in other files. For some reason when I compile my program and I get error, which is regarding initialization function (TableCreate();) of hash table. When I remove this part of the code from main function and execute, it works fine but then I get segfault when i try to add something to the hash table.

I believe my hash table code has nothing to do with this errors because my hash table code is based upon examples of Hash table code which was provided to us by our professor

I'm using GCC compiler.

Please help me solve this issue.

Error Message

src/sshell.c: In function âmainâ:
src/sshell.c:34: warning: implicit declaration of function âTableCreateâ
src/sshell.c:34: warning: assignment makes pointer from integer without a cast

sshell.c

        #include "parser.h"
    #include "shell.h"
    #include "hash_table.h"
    #include "variables.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>



int main(void){

    char input[1000], sInput[1000]; // String to get user input
    int count=1, len; // num=0;





    struct Table *t;
    t = TableCreate(); //create the table




    int while_track;
    FILE *ptr_file;
    ptr_file =fopen(".simpleshell_history","a");
    fclose(ptr_file);
    printf("\nWelcome to the sample shell!  You may enter commands here, one\n");
    printf("per line.  When you're finished, press Ctrl+D on a line by\n");
    printf("itself.  I understand basic commands and arguments separated by\n");
    printf("spaces, redirection with < and >, up to two commands joined\n");
    printf("by a pipe, tilde expansion, and background commands with &.\n\n");

    printf("\npssh$ ");
    while (fgets(input, sizeof(input), stdin)) {
          strcpy(sInput, input);
          len = strlen(input);
          if( input[len-1] == '\n' ){
              input[len-1] = '\0';
          }
          while_track = 1;  // used to keep track of loop
          while (while_track == 1){
                count+=1;
                if (strcmp(input, "history")==0){       
                   while_track = History(); // print history function call
                }else if (strcmp(input, "!!")==0){
                      while_track = Previous(input); // execute previous function call
                }else if (strncmp(input, "!",1)==0){ // !string and !number sort

                      if(isdigit(input[1])){              
                         while_track = Number(input);
                      } else {
                         while_track = String(input);
                      }  

               }else { // if the user entry was not any of specfic comnnad then pass the command to parse to execute
                     other(input,t);
                     parse(input);

                     while_track = 0;
               }     
          }
          HistoryFile(sInput); // A function call to recode commands entered by the user into a file
          printf("\npssh$ ");
    }
    return 0;
}

hash_table.c

#include "hash_table.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>



void feedData(char * var, char * val, struct Table *t){
     //const char * key=0;
     printf("\n In FeedData Function\n");
     Table_add(t, var, val);
     printf("\nInside Feed Function -- Veriable: %s Value: %s\n",var, val);
}

unsigned int hash(const char *x) {
         printf("\n In Hash\n");
         int i;
         unsigned int h = 0U;
         printf("\n In Hash - Before for loop\n");
         for (i=0; x[i]!='\0'; i++)
             printf("\n In Hash - In for loop %d \n", i);
             h = h * 65599 + (unsigned char)x[i];
             printf("\n In Hash - In for loop - after calculation \n");
         unsigned int g;
         g = h % 1024;   
         printf("\n In Hash - In for loop - before return: %u \n",g);     
         return g;
}

struct Table *Table_create(void) {
       printf("\n In Table_create\n");
       struct Table *t;
       t = (struct Table*)calloc(1, sizeof(struct Table));
       return t;
}

void Table_add(struct Table *t, const char *key, char * val){

      printf("\n In Table_add\n");
      struct Node *p = (struct Node*)malloc(sizeof(struct Node));
      int h = hash(key);
      printf("\n In Table_add - after hash key\n");
      //p->key = *key;
      strcpy(p->key,key);
      printf("\n In Table_add - after first key\n");
      strcpy(p->value,val);
      printf("\n In Table_add - after Value\n");
      p->next = t->array[h];
      printf("\n In Table_add - after p->next\n");
      t->array[h] = p;
      printf("\n In Table_add - after t->array[h] = p\n");
}
/*
int Table_search(struct Table *t, const char *key, int *value){
    struct Node *p;
    int h = hash(key);  //---------------------------------------------------------------------
    for (p = t->array[h]; p != NULL; p = p->next)
    if (strcmp(p->key, key) == 0) {
       *value = p->value;
       return 1;
    }
    return 0;
}
*/
/*
void Table_free(struct Table *t) {
     struct Node *p;
     struct Node *nextp;
     int b;
     for (b = 0; b < BUCKET_COUNT; b++)
         for (p = t->array[b]; p != NULL; p = nextp) {
             nextp = p->next;
             free(p);
         }
     free(t);
}
*/

hash_table.h file code

    #ifndef HASH_TABLE_H
#define HASH_TABLE_H

struct Table *Table_create(void);
void Table_add(struct Table *t, const char *key, char * val);
unsigned int hash(const char *x);
void feedData(char * var, char * val, struct Table *t);


enum {BUCKET_COUNT = 1024};
struct Node {
       char key[1000];
       char variable[1000];
       char value[1000];
       struct Node *next;
};


struct Table {
       struct Node *array[BUCKET_COUNT];
};


#endif
Était-ce utile?

La solution

Warning 1: You are calling TableCreate while your function name is Table_create

Warning 2: After looking at new identifier followed by (, compiler assumes it is a function that takes variable number of arguments and returns int.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top