HI ,
I am trying to execute below simple program. But i am getting "Segmentation fault" after selecting choice 1.
Please hlep me regarding this.
Code:
#include<stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *addr;
};
struct node *first;
int main()
{
int choice;
printf("%d \n",first);
while(1)
{
printf("enter the choice\n");
printf (" 1.display\n 3.quit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
display();
break;
case 3:
quit();
}
}
return 0;
}
int display()
{
struct node *temp;
temp=(struct node *)malloc(1 * sizeof(struct node));
printf("entered into display function\n");
if (temp == NULL)
{
printf("Memory is not allocated to create new node\n");
return 0;
}
printf ("%d\n",temp);
printf ("%d\n",temp);
printf ("%d\n",first->data);
if ( first->addr == NULL)
{
printf ("List is empty\n");
return 0;
}
else
{
temp=first;
printf ("The elements in Linked list are\n");
while (temp->addr !=NULL)
{
printf ("%d\n",temp->data);
temp = temp->addr;
}
}
return 0;
}
int quit()
{
printf("entered into exit\n");
exit (0);
}
o/p:
enter the choice
1.display
3.quit
1
entered into display function
145903624
145903624
Segmentation fault
Regards
Koti