Click here to Skip to main content
13,664,548 members
Rate this:
 
Please Sign up or sign in to 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:

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 14-Nov-17 1:42am
Updated 14-Nov-17 2:11am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Quote:
first=temp;
should be instead
first=tempfirst;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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;

    // ...
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180810.1 | Last Updated 14 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100