Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
please tell me why am i getting segmentation fault in this code.I am getting an error at 72nd line. I wanted to make another node link though linkA but I can't. When ever I try to access the node by writing first->linkA=temp i get the error

What I have tried:

C++
typedef struct node
        {
          char word[100];
          char meaning[100];
          struct node *link;
        }*wordptr;
        
        typedef struct alphanode
        {
        char word[100];
        char meaning[100];
        struct alphanode *linkA;
        
        struct alphanode *linkB;
        }*alphabets;
        
        
        int main()
        {
            int i=1;
            alphabets first, temp;
        
            alphabets tempfirst=(alphabets)malloc(sizeof(struct alphanode));
        
            tempfirst->linkA=NULL;
            tempfirst->linkB=NULL;
        
            first=temp;
        
        
            //creating 26 linked nodes
            for(i=1;i<=26;i++)
            {
              temp=(alphabets)malloc(sizeof(struct alphanode));
              tempfirst->linkB=temp;
        
        
              temp->linkA=NULL;
              temp->linkB=NULL;
        
              tempfirst=temp;
            }
        
        
            enterAlpha("bcd", first);
            displayMyDictionary(first);
        
            printf("Hello world!\n");
            return 0;
        }
        
        void enterAlpha(char str[], alphabets first)
        {
        int i=0;
        
           //takes to the alphate starting node
          for(i=0;i<=((int)str[0]-'a');i++)
          {
            first=first->linkB;
          }
            
          alphabets temp = (alphabets)malloc(sizeof(struct alphanode));         
           first->linkA=temp;
           first=temp;
            printf("%s", first->word);
           
        
        }
Posted
Updated 14-Nov-17 2:11am
v3

Quote:
first=temp;
should be instead
C
first=tempfirst;
 
Share this answer
 
Your first pointer is invalid because it is initialised with an indeterminate value:
// These are not initialised and contain therefore indeterminate values
// It would be better to set them to NULL and use appropriate checks
//  when using them later
alphabets first, temp;
        
alphabets tempfirst=(alphabets)malloc(sizeof(struct alphanode));
        
tempfirst->linkA=NULL;
tempfirst->linkB=NULL;

// Because temp is not initialised, first contains an indeterminate value too        
first=temp;

// ...

// Passing invalid pointers here
enterAlpha("bcd", first);
displayMyDictionary(first);

Better code would do something like this:

#include <assert.h>

int main()
{
    alphabets first = NULL;
    alphabets temp = NULL;
    // ...
}

void enterAlpha(char str[], alphabets first)
{
    // Break with debug builds
    assert(str != NULL);
    assert(first != NULL);

    // Avoid seg faults with release builds
    if (NULL == first)
        return;

    // ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900