Hi, I have the following code that is meant to serve as a t9 program like the one in mobile phones. It initially loads a dictionary file, stores the values in a dictionary, and prompts the user to enter a string of numbers between 2 and 9. For some reason it is not working, I keep getting a segmentation fault pointing to the 'root' variable in the print function, even when I enter number sequences that match particular characters. I am wondering I did not store the values properly or I am just accessing the values in the wrong way. Any help will be appreciated, as I am very new to c. Thanks

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int count;
int lenght = 0;
struct list_item *ptr;
void print(struct list_item *  suggest);
struct list_item *head = NULL;
void add_item(struct list_item *item);
struct list_item *allocate_item(struct list_item *temp, char value[]);


//struct for the linked list
struct list_item{
  char data[128];
  struct list_item *next;
};


//new struct with 8 child nodes
struct t9
{
  struct t9  * node[8];
  struct list_item * list;
};


struct list_item *allocate_item(struct list_item *temp, char *value)
{
  // struct list_item *temp;
  temp = (struct list_item*)malloc(1*sizeof(struct list_item));
  if (temp==NULL)
    {
      puts("malloc() failed to give us memory! exiting...\n");
      exit(1);
    }
  int i;
  strcpy(temp->data, value);
  temp->next = NULL;
  return temp;
}
//adds the items
void add_item(struct list_item *item){
  if (head != NULL)
    item->next = head;
  head = item;
}

//prints the list
void print(struct list_item * suggest)
{
  //  count=200;

  char file[count][128];
  int i=0;
  printf("\n");

  for (suggest=head;suggest!=NULL; suggest = suggest->next)
    {
      strcpy(file[i], suggest->data);
      i++;
    }
  i=count-1;
  while(i>=0)
    {
      printf("%s\n", file[i]);
      i--;
    }

  if (suggest!= NULL)
    {
      printf("Suggestion: %s\n", ptr->data);
    }
  else
    {
      printf("There are no suggestions for your character combination\n");
    }
}
//initializes a new strct, making the child nodes 0
struct t9 * t9_new()
{
  struct t9 * r =  (struct t9 *)malloc(sizeof(struct t9));
  return r;
}


//frees the dynamically allocated memory
void t9_free(struct t9 * root)
{
  if(root)
    {
      int i=0;
      for(;i<8;i++)
        {
          t9_free(root->node[i]);
        }
      free(root);
    }
}

//inserts a new value into the trie
struct t9 * t9_insert(struct t9 * root ,char *val)
{

  //if there is no root, it creates a new root
  if(!root)
    {
      root = t9_new();
    }

  //if there is no value, it returns the trie as is
  if(!*val)
    {
      return root;
    }

  int i;

  //allocates space for the child nodes of a particular root
  for(i=0;i<8;i++)
    {
      root->node[i] = (struct t9 *)malloc(sizeof(struct t9));
    }

  //gets a numerical value from 0-25 for each character
  char c = *val - 'a';

  if (c<=2)
    {
      ptr = allocate_item(root->node[0]->list,val);
      add_item(ptr);
      count++;
      root->node[0] = t9_insert(root->node[0] ,++val);
    }

  if (c>=3 && c<=5)
    {

      ptr = allocate_item(root->node[1]->list,val);
      add_item(ptr);
      count++;
      root->node[1] = t9_insert(root->node[1] ,++val);
    }

  if (c>=6 && c<=8)
    {
      ptr = allocate_item(root->node[2]->list,val);
      add_item(ptr);
      count++;
      root->node[2] = t9_insert(root->node[2] ,++val);
    }

  if (c>=9 && c<=11)
    {
      ptr = allocate_item(root->node[3]->list,val);
      add_item(ptr);
      count++;
      root->node[3] = t9_insert(root->node[3] ,++val);
    }

  if (c>=12 && c<=14)
    {
      ptr = allocate_item(root->node[4]->list,val);
      add_item(ptr);
      count++;
      root->node[4] = t9_insert(root->node[4] ,++val);
    }

  if (c>=15 && c<=18)
    {
      ptr = allocate_item(root->node[5]->list,val);
      add_item(ptr);
      count++;
      root->node[5] = t9_insert(root->node[5] ,++val);
    }

  if (c>=19 && c<=21)
    {
      ptr = allocate_item(root->node[6]->list,val);
      add_item(ptr);
      count++;
      root->node[6] = t9_insert(root->node[6] ,++val);
    }

  if (c>=22 && c<=25)
    {
      ptr = allocate_item(root->node[7]->list,val);
      add_item(ptr);
      count++;
      root->node[7] = t9_insert(root->node[7] ,++val);
    }

  //  root->node[c] = t9_insert(root->node[c] ,++val);

  return root;

}


struct t9 * find(struct t9 * root, char *combo)
{


  char c  = *combo;
  if (c == '2')
    {
      //      root = root->node[0];
      find(root->node[0],++combo);
    }
  else if (c == '3')
    {
      root = root->node[1];
      find(root,++combo);
    }
  else if (c == '4')
    {
      root = root->node[2];
      find(root,++combo);
    }
  else if (c == '5')
    {
      root = root->node[3];
      find(root,++combo);

    }
  else if (c == '6')
    {
      root = root->node[4];
      find(root,++combo);
    }
  else if (c == '7')
    {
      root = root->node[5];
      find(root,++combo);
    }
  else if (c == '8')
    {
      root = root->node[6];
      find(root,++combo);
    }
  else if (c == '9')
    {
     root = root->node[7];
     find(root,++combo);
    }
  else if(c=='*')
    {
      printf("Next suggestion");
      find(root,++combo);
    }
  else
    {
      print(root->list);
    }
}


int main()
{
  struct t9 * root = (struct t9 *)0;
  //  struct t9 * root1 = root;
  char a[100],b[100], c[100];
  int i;
  //opens dictionary file
  FILE *fp = fopen("t.dic","r");

  //scans dictionary file
  while(!feof(fp))
    {
      fscanf(fp,"%s",&a);

      if(a[0]=='0')
        {
          break;
        }
          root=t9_insert(root,a);
    }

  //prompt till user quits program
  while(1)
    {
      //prompts user to enter keys
      printf("Enter t9 key combinations from 2-9:");

      //scans users input
      scanf("%s",&b);

      find(root,b);
      //       t9_search_mob(c,root,0,b);
           }

  //free the roots when the program is done
  t9_free(root);

  return 0;
}

Please how do I delete this thread, my professor says that I could get in trouble for posting my code online, as it violates my school's computer sciences policy.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.