I am currently experiencing a problem with creating my linked list in my function createDeck
. It keeps telling me that my pointer temp
is being dereferenced. I have tried allocating it in main
but it didn't seem to do anything. I've attached samples of my code below.
typedef struct card_s {
char suit[9];
int value;
struct card_s *next, *previous;
} card;
void createDeck(card *p, card **hl, card **hr, FILE *inp) {
card *temp = (card *)malloc(sizeof(card));
while (fscanf(inp, "%d %s", &temp->value, temp->suit) != EOF) {
if (*hl == NULL) { //beginning of list
temp->next = NULL;
temp->previous = NULL;
*hl = temp; // headp equals temp if null
*hr = temp;
} else
if (p->next == NULL) { //end of list
p->next = temp;
temp->previous = p;
*hr = temp;
temp->next = NULL;
}
}
}
void printDeck(card *head) {
while (head != NULL) {
printf("Value: %d Suit: %s", head->value, head->suit);
}
}
int main(void) {
FILE *inp;
card *headl = NULL, *headr = NULL;
inp = fopen("cards.txt", "r");
if (inp == NULL) {
printf("can't read file");
return -1;
}
createDeck(headr, &headl, &headr, inp);
printDeck(headl);
return 0;
}