EmbLogic's Blog

linklidt and scull

problem is while traversing it gives the segmentation fault.

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

struct linklist{
char **data;
struct linklist *next;
}*start,*temp,*last;
int main()
{
int i,j,m,l;
int quantams;
char y;
start=(struct linklist*)malloc(sizeof(struct linklist));
temp=(struct linklist*)malloc(sizeof(struct linklist));
//last->data=malloc(sizeof(char*)*);
//printf(“enter the dat in the first node\n”);
start->next=NULL;
//scanf(” %d”,&start->data);
temp=start;
j=0;
printf(“do u wana linklist press y\n”);
scanf(” %c”,&y);
while(y==’y’||y==’Y’)

{
printf(“this is %d node\n”,j+1);
last=(struct linklist*)malloc(sizeof(struct linklist));
last->data=(char **)malloc(sizeof(char *));
for(m=0;m<=1;m++)
{
last->data[m]=(char*)malloc(sizeof(char));

for(l=0;l<=1;l++)
{
printf(“row no %d: column no %d”,m,l);
printf(” enter the information\n”);
scanf(“%d”,&quantams);
//last->data[m][l]=quantams;
*(*(last->data+m)+l)=quantams;
}
}
last->next=NULL;
printf(“last->next=NULL\n”);
temp->next=last;
printf(“temp->next=last\n”);
temp=last;
printf(“temp=last\n”);
j++;
printf(“do you wana continue press y or Y\n”);
scanf(” %c”,&y);
}
j=0;
temp=start;
while(temp->next!=NULL)
{
printf(“this is %d node\n”,j);
for(m=0;m<=1;m++)
{

for(l=0;l<=1;l++)
{
printf(“row no %d :: column no %d”,m,l);
printf(” %d\n”,*(*(temp->data+m)+l));
}
}
temp=temp->next;
j++;
}
return 0;
}

One Response to linklidt and scull

  1. zaffer says:

    hah hah when we traverse if we carefully notice the traverse is
    printf(” %d\n”,*(*(temp->data+m)+l));
    in start there is no m and l so when we execute *(*(start->data+m+l));
    becoz m and l are not defined for start and when we traverse start =temp for first function.
    it gives us segmentation fault

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>