I am practicing the c language and am trying to create a linked list with structures that tells you if the day of the week entered is in the list.
#include <stdio.h>
#include <stdbool.h>
bool isTrue=1, *ptrisTrue=&isTrue;
struct weekday {
char *ptrday;
struct weekday *next;
} sunday, monday, tuesday, wednesday, thursday, friday, saturday;
struct weekday *head=&sunday;
struct weekday *cursor;
struct weekday *ecursor;
void matchtest(char *eday, struct weekday *head, struct weekday *cursor) {
cursor=head;
while (cursor!=(struct weekday *)0){
while (*eday!='\0') {
if (*eday!=*cursor->ptrday)
*ptrisTrue=0;
++eday; ++cursor->ptrday;
}
if (*ptrisTrue==1)
printf("Yes, %s is in the list\n", cursor->ptrday);
cursor=cursor->next;
}
}
int main (void) {
char enteredday[]="Monday", *ptreday=enteredday;
sunday.ptrday="Sunday"; monday.ptrday="Monday"; tuesday.ptrday="Tuesday";
wednesday.ptrday="Wednesday"; thursday.ptrday="Thursday";
friday.ptrday="Friday"; saturday.ptrday="Saturday";
sunday.next=&monday; monday.next=&tuesday; tuesday.next=&wednesday;
wednesday.next=&thursday; thursday.next=&friday; friday.next=&saturday;
saturday.next=(struct weekday *)0;
head->next=&sunday;
printf("This is a test to see if a day is in the list.\n");
matchtest(ptreday, head, cursor);
return 0;
}
(I will put a scan function in for "enteredday," for now it is set to Monday.) This program is nowhere near the most efficient one, but I am just testing out the different concepts that I have already learned. When I use breakpoints to pinpoint the issue of the program, I see that when I try to set the cursor to point to the next structure at the end of the first while statement in the "matchtest" function (cursor=cursor->next;), the cursor value for the day member of the structure is set to two quotation marks (""), instead of "Monday". How can I fix this issue?
isTrueto zero on the first mismatch and never set it non-zero anywhere else in the code.boolvariables should normally be assignedfalse(in preference to0) ortrue(in preference to1). A nameisTrueboggles the mind a bit; what 'is true'? And aptrIsTrueis more mind-bending still. Do you need to repoint it atisFalsesometimes? It is worrying nomenclature.