I created linked list of my structure, but for some reason every time I add another link it changes head address, but I want y head address be first entry. this is my code:
struct checkPoints *tgh = NULL;
struct checkPoints **linkedlist = &tgh;
struct checkPoints *cp = malloc(sizeof (struct checkPoints));
chPo = fopen(fileName, mode);
if (chPo == NULL) {
printf("Can't find the files.");
exit(1);
} else {
for (i = 0; i < lines; i++) {
fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
cp->next = NULL;
if (*linkedlist == NULL) {
printf("ONCE");
*linkedlist = cp;
} else {
struct checkPoints *new = *linkedlist;
while (new->next != NULL) {
new = new->next;
}
new->next = cp;
}
}
}
every fscanf occurs it changes head address to next, any ideas?
Head address changes after this line: fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
The structure is this:
struct checkPoints{
char dropOut;
int currentPoint;
int competitor;
int hour;
int minute;
struct checkPoints *next;
};
The problem here is that you do not allocate new nodes, you only have one node that you change over and over again. You need to allocate the node inside the loop.