I'm working on a List example in C where new nodes are pushed on to the end of the stack. I keep getting a Bus Error: 10
when I try to push the new node on to the end. Here is my push function:
void push(struct node *tail, struct node *newNode) {
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}
and I call it using push(tail, newNode);
Also here is my struct if necessary:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};
And here is the main function showing code leading to push()
int main()
{
char inputString[50];
int timeHour, timeMin;
struct node *head;
struct node *tail;
while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF) {
scanf("%s", inputString);
if (strcmp(inputString, "enqueue") == 0) {
if (head == NULL) {
head = malloc(sizeof(struct node));
head->hour = timeHour;
head->minute = timeMin;
// get name
scanf("%s", inputString);
head->name = malloc(strlen(inputString)+1);
strcpy(head->name, inputString);
tail = head;
printEnqueue(head);
} else {
struct node *newEntry = malloc(sizeof(struct node));
newEntry->hour = timeHour;
newEntry->minute = timeMin;
// get name
scanf("%s", inputString);
newEntry->name = malloc(strlen(inputString)+1);
strcpy(newEntry->name, inputString);
push(tail, newEntry);
printEnqueue(newEntry);
}
} else {
pop(&head, timeHour, timeMin);
}
}
return 0;
}