-1
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;
void insert(int a)
{
    struct node* temp=(struct node*)malloc(sizeof(struct node));
    struct node* temp1;
    if(head=NULL)
    {
        temp->data=a;
        temp->next=NULL;
        head=temp;
        return;
    }
    {
        temp1=head;
        temp->data=a;
        while(temp1->next!=NULL)
        {
            temp1->next=temp;
        }
    }
}
void print()
{
    struct node* temp2;
    temp2=head;
    while(temp2->data!=NULL)
    {
        printf("the data are %d",temp2->data);
    }
}
int main()
{
    int n,a,i;
    printf("how many number");
    scanf("%d",&n);
    head=NULL;
    for(i=0;i<=n;i++)
    {
        printf("enter the no. to store");
        scanf("%d",&a);
        insert(a);
        print();
    }
}

I don't know what is wrong with this program. Everytime i try to compile this program an error occurs: "segmentation fault (core dumped)". I'm doing this program in Ubuntu. What does segmentation fault mean and how can I fix it?

dat3450
  • 954
  • 3
  • 13
  • 27
khan
  • 1
  • 1
  • 2
    1) `if(head=NULL)` --> `if(head==NULL)` – BLUEPIXY Aug 17 '17 at 01:24
  • 2
    A segmentation fault is the result of a memory access violation. Please, please, please consider correctly formatting your code. – ad absurdum Aug 17 '17 at 01:28
  • 3) `while(temp2->data!=NULL) { printf("the data are %d",temp2->data); }` --> `while(temp2!=NULL) { printf("the data are %d",temp2->data); temp2 = temp2->next; }` – BLUEPIXY Aug 17 '17 at 01:29
  • 2) `temp1=head; temp->data=a; while(temp1->next!=NULL) { temp1->next=temp; }` --> `temp1=head; temp->data=a; temp->next = NULL; while(temp1->next!=NULL) { temp1 = temp1->next; } temp1->next = temp;` – BLUEPIXY Aug 17 '17 at 01:35
  • 4) `i<=n` --> `i < n` – BLUEPIXY Aug 17 '17 at 01:38
  • 1
    *Every time i try to **compile** this program an error occurs: "segmentation fault (core dumped)"*. Maybe `sudo apt-get reinstall gcc`? – iBug Aug 17 '17 at 04:22
  • [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Aug 17 '17 at 04:35

1 Answers1

0

Maybe next time you should use a debugger.

  #include<stdio.h>
  #include<stdlib.h>
  struct node
  {
      int data;
      struct node *next;
  };
  struct node *head;
  void insert(int a)
  {
>     struct node* temp=malloc(sizeof(struct node));
      struct node* temp1;
>     if(head==NULL)
      {
          temp->data=a;
          temp->next=NULL;
          head=temp;
          return;
      }
>     else
      {
          temp1=head;
          temp->data=a;
>         temp->next=NULL;
          while(temp1->next!=NULL)
          {
>             temp1=temp1->next;
          }
>         temp1->next=temp;
      }
  }
  void print()
  {
      struct node* temp2;
      temp2=head;
>     while(temp2!=NULL)
      {
          printf("the data are %d",temp2->data);
>         temp2=temp2->next;
      }
  }
  int main()
  {
      int n,a,i;
      printf("how many number");
      scanf("%d",&n);
      head=NULL;
>     for(i=0;i<n;i++)
      {
          printf("enter the no. to store");
          scanf("%d",&a);
          insert(a);
          print();
      }
  }
iBug
  • 35,554
  • 7
  • 89
  • 134