After the second enter ,the program stop working,any idea what I did wrong,Ithink i have problem in the loop ,but I can't see it.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef
struct node{
char name[20];
int id;
struct node *next;
}student;
void printList(student *ptr_head);
int main()
{
student *first,*current;
char ime[20];
int ID,i;
printf("Enter name: ");
scanf("%s",ime);
printf("Enter id: ");
scanf("%d",&ID);
first=malloc(sizeof(student));
strcpy(first->name,ime);
first->id=ID;
first->next=NULL;
first=current;
for (i=0;i<3;i++)
{
printf("Enter name: ");
scanf("%s",ime);
printf("Enter id: ");
scanf("%d",&ID);
student *temp;
temp=malloc(sizeof(student));
strcpy(temp->name,ime);
temp->id=ID;
temp->next=NULL;
current->next=temp;
current=temp;
} printList(first);
getch();
return 0;
}
void printList(student *first)
{
while(first){
printf("%s : %d \n",first->name,first->id);
first =first->next;
}
}