user3011439
user3011439

Reputation: 35

C linked list pointer issue

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?

Upvotes: 0

Views: 121

Answers (2)

Armali
Armali

Reputation: 19395

but why is the cursor=cursor->next; statement not working?

It is working - but by the assignment head->next=&sunday in main() you created an endless link loop, because *head is the object sunday and then sunday->next points back to sunday.

Just drop that head->next=&sunday line in main(); you already assigned sunday.next=&monday.

Upvotes: 0

Mohammad Jafar Mashhadi
Mohammad Jafar Mashhadi

Reputation: 4251

This is because this line of code:

++cursor->ptrday;

You are incrementing ptrday till you reach NULL character, since C strings are implemented using arrays and the name of array is equivalent to a pointer to first member of array, when you increment the pointer till you reach \0 you ignore all characters before \0.

Memory is like this:

  _______________
  |M|o|n|d|a|y|\0|
  ________________
   ^ Where cursor->ptrday used to and should point to, 
               ^ Where cursor->ptrday points to after the second while statement

To solve this you can use strcmp function or change the while loop like this:

char* p = cursor->ptrday;
*ptrisTrue = 1;
while (*eday!='\0') {
    if (*eday != *p)
        *ptrisTrue=0;
    ++eday; ++p;
}

Also note that you forgot to reset *ptrisTrue to true.

Upvotes: 2

Related Questions