I am new to programming.The other day I was playing around with structures and pointers...I was getting errors.I tried to rectify them.It got rectified...But I cant justify why there was an error at the first place.Please help me...
struct node{
int data;
struct node*next;
};
int main(){
struct node *newnode=NULL;
struct node *start=NULL;
newnode=start=(struct node*)malloc(sizeof(struct node));
newnode->data=1;
//code snippet
newnode->next=NULL;
newnode=newnode->next;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=2;
start=start->next;//error probably as start->next is perceived as NULL Address
printf("%d",start->data);
return 0;
}
when replacing code snippet with this code
newnode->next=(struct node*)malloc(sizeof(struct node));
newnode=newnode->next;
newnode->data=2;
start=start->next;
printf("%d",start->data);
error dissapers..How does one justify this ?