Aditya Bachhawat
Aditya Bachhawat

Reputation: 35

link list segmentation fault

typedef struct node{
int data;
struct node *link;
}nd;

nd *head=NULL , *ahead=NULL;

void create_node(int item) {
    nd *new, *temp;
    new = (nd*)malloc(sizeof(nd));
    new->data=item;
    new->link=NULL;
    if(head==NULL) {
        head=new;
    }
    else {
        temp=head;
        while(temp->link!=NULL) {
            temp=temp->link;
        }
        temp->link=new;
    }

}

void alpha_check(int size) {
    int i,j,num;
    nd *ti , *tj;
    ti=tj=head;
    for(i=1 ; i<=size ; i++) {
        for(j=1 ; j<=size ; j++) {
            num = ((ti->data)*10)+(tj->data);
            tj=tj->link;

            /*if(num>=65 && num<=90) {
                   printf("\n->%d",num);
              }*/
        }
     //ti=ti->link;
    }
}

void traverse(nd *thead) {
    while(thead->link!=NULL) {
        printf("%d ",thead->data);
        thead=thead->link;
    }
    printf("%d ",thead->data); 
}

So the only problem in the above code lies in the function alpha_check() where i want the variable tj point to the next node. Instead of pointing to the next node its giving me Segmentation fault (core dumped). please explain why can't I make tj point to the next node.

Upvotes: 0

Views: 71

Answers (1)

Joshua Newhouse
Joshua Newhouse

Reputation: 171

Segmentation fault is a signal to the kernel that your program is accessing memory that it doesn't have have permission to causing the kernel to terminate your program. This usually means you are exceeding the bounds of an array or in your case you are dereferencing a pointer that is pointing to something it shouldn't be. Like the others alluded to in their comments you need to have a different type of constraint while traversing a linked list than you would when traversing an array. You need to traverse while checking that the node pointer is not NULL rather than doing some fixed size in a for loop.

I've made a change to your alpha_check procedure and added a main for testing it. It works as you would expect.

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

typedef struct node {
    int data;
    struct node* link;
} nd;

nd *head=NULL , *ahead=NULL;

void create_node(int item) {
    nd* new,* temp;
    new = (nd*)malloc(sizeof(nd));
    new->data = item;
    new->link = NULL;

    printf("%d %p\n", new->data, new);

    if(head == NULL) {
        head = new;
    }
    else {
        temp = head;
        while(temp->link)
            temp = temp->link;
        temp->link = new;

    }
}

void alpha_check(int size) {
    int i,j,num;
    nd* ti ,* tj;
    ti = tj = head;

    for(i = 1 ; i <= size ; i++) {
        while(tj) {
            num = ti->data * 10 + tj->data;
            tj = tj->link;

         //if(num>=65 && num<=90)
         //{
            printf("\n->%d",num);
            printf(" %p\n", tj);
         //}
     }
     //ti=ti->link;
    }
}

void traverse(nd* thead) {
    while(thead->link) {
        printf("%d ", thead->data);
        thead = thead->link;
    }
    printf("%d ", thead->data);
}

int main(void) {
    create_node(10);
    create_node(1);
    create_node(5);

    alpha_check(2);
    return 0;
}

Upvotes: 1

Related Questions