I have this code that creates a linked list but I get a segmentation fault error. Any solutions?
struct node *temp = (struct node*)malloc(sizeof(struct node));
char *a = title_parser(bytes, temp);
head = temp;
temp = (struct node*)malloc(sizeof(struct node));
title_parser(a, temp);
puts(a); // Is NULL
struct node* temp1 = head;
while(temp1->link != NULL)
{
temp1 = temp1->link;
}
temp1->link = temp;
EDIT: After the program traverses the function title_parser
, memory is allocated for temp again but when the second time title_parser
is called, this is when segmentation fault occurs.
This is my code for title_parser
char* title_parser(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "<title>");
if (ptr) {
/* Skip over the found string */
ptr += 7;
char *ptr2 = strstr(ptr, "</title>");
if (ptr2) {
char* output = malloc(ptr2 - ptr + 1);
memcpy(output, ptr, ptr2 - ptr);
output[ptr2 - ptr] = 0;
if(strcmp(output,"TechCrunch")!=0 && strcmp(output,"VentureBeat")!=0){
temp->title = output;
temp->link = NULL;
puts(temp->title);
free(output);
char *load = pubdate_parser(ptr2, temp);
return load;
}
else{
title_parser(ptr2, temp);
}
}
}
}
EDIT: Another problem is that when title_parser
returns load, I print a
in main and the output is NULL.