i have a list of quads and they have a label starting from 1. the backpatch is taking a list structure which points at some quads. i want backpatch to update those quads, putting z on the char * fourth and then emptying l so i can put other quads later.I get seg.fault
in backpatch's strcpy
although I have allocated memory for the char * z
and char * fourth
. Does anybody know why does that happens?
struct quad {
char *label; //5
char *first; //30
char *second;
char *third;
char *fourth;
struct quad *next;
};
struct list {
struct quad *quadlist;
struct list *nextlist;
};
void backpatch(struct list *l, char * z) {
struct list *temp = (struct list*) malloc(sizeof (struct list));
temp->nextlist = (struct list*) malloc(sizeof (struct list));
temp->quadlist = (struct quad*) malloc(sizeof (struct quad));
temp->quadlist->fourth = (char*)malloc(30 * sizeof (char));
l->nextlist = (struct list*) malloc(sizeof (struct list));
temp = l;
//z=(char*)malloc(sizeof(struct list))
while (temp->nextlist != NULL) {
strcpy(temp->quadlist->fourth, z);
temp = l->nextlist;
}
strcpy(temp->quadlist->fourth, z);
free(temp);
free(l);
}
even if i only keep the
while (l->nextlist != NULL) {
strcpy(l->quadlist->fourth, z);
l = l->nextlist;
}
strcpy(l->quadlist->fourth, z);
free(l);
part, its also seg.fault...