Thread: Array of Linked Lists Problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    36

    Array of Linked Lists Problem

    I am making an array of linked lists, but for some reason its giving me a segmentation error.

    Now i know it's not accessing outside the array because i've checked. Yet it only seems to have a problem with that one line noted.

    struct NODE
    {
    char word[WORDSIZE];
    struct NODE *next;
    };

    struct LIST
    {
    struct NODE listTop;
    };




    void checkMistakes()
    {
    char *token;
    char word[WORDSIZE];
    char lowerWord[WORDSIZE];
    int hashValue;
    int i=0;
    // int found=FALSE;
    struct NODE *node;

    lineCounter++; //increment the line currently reading

    token=strtok(line,delim);
    while(token!=NULL)
    {
    strcpy(word,token);

    for(i=0;i<strlen(word) && word[i]!='\0';i++)
    {
    lowerWord[i]=(char)tolower(word[i]);
    }

    //puts a null character at the end of the word so array knows where word ends
    lowerWord[i]='\0';

    hashValue=hash(lowerWord);
    printf("%i %s",hashValue,word);

    //-----------------------------------------------------------------
    //this is the line that keeps having the segmentation error
    *node=table[hashValue].listTop;
    //-----------------------------------------------------------------
    if(table[hashValue].listTop.word[0]!='\0')
    {


    //search for the word in the list
    while(node->word[0]!='\0' && found!=TRUE)
    {
    if(strcmp(node->word,word)==0)
    //then found
    found=TRUE;
    else
    //increment
    node = node->next;
    }

    //printf("%s %s\n",word,lowerWord);
    }

    // if(found==0)
    // {
    // enter(word);
    // }


    token=strtok(NULL,delim);
    }
    }

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You don't set node to point to anywhere, so dereferencing it means you're accessing memory your program doesn't own. Try this instead :-)
    Code:
    node = &table[hashValue].listTop;
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked lists inserting problem
    By sara.stanley in forum C Programming
    Replies: 5
    Last Post: 04-02-2006, 01:26 AM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM