I am getting segmentation fault when using ptr != NULL in shareRecord while loop but it seems to work when I use ptr->next != NULL.
So when I use ptr->next != NULL and uncomment the lines in shareRecord, the code works. But why doesn't it work using ptr != NULL only.
Kindly let me know what the issue is.
struct data *shareRecord(struct data *head, struct data *fileline){
struct data *ptr;
ptr = head;
int flag = 0;
while(ptr != NULL){
if((strcmp(ptr->symbol, fileline->symbol) == 0) && (strcmp(ptr->type, fileline->type) == 0 )&& (ptr->time == fileline->time)){
flag = 1;
break;
}
ptr = ptr->next;
}
//if((strcmp(ptr->symbol, fileline->symbol) == 0) && (strcmp(ptr->type, fileline->type) == 0 )&& (ptr->time == fileline->time)){
//flag = 1;
//}
if(ptr->next == NULL && flag == 0){
return NULL;
}
else
return ptr;
}
struct data *create_data(struct data *head, struct data *fileline){
struct data *newdata, *ptr1, *ptr2;
struct price *newprice, *priceptr1;
newprice = (struct price *)malloc(sizeof(struct price *));
newprice->amt = fileline->price->amt;
newprice->next = NULL;
if(head == NULL){
newdata = (struct data *)malloc(sizeof(struct data));
newdata->time = fileline->time;
strcpy(newdata->symbol, fileline->symbol);
strcpy(newdata->type, fileline->type);
newdata->price = newprice;
newdata->next = NULL;
head = newdata;
}
else{
ptr1 = head;
ptr2 = head;
if((ptr1 = shareRecord(head, fileline)) != NULL){
priceptr1 = ptr1->price;
while(priceptr1->next != NULL){
priceptr1 = priceptr1->next;
}
priceptr1->next = newprice;
}
else{
while(ptr2->next != NULL){
ptr2 = ptr2->next;
}
newdata = (struct data *)malloc(sizeof(struct data));
newdata->time = fileline->time;
strcpy(newdata->symbol, fileline->symbol);
strcpy(newdata->type, fileline->type);
newdata->price = newprice;
newdata->next = NULL;
ptr2->next = newdata;
}
}
return head;
}