0

Similar question here . What I want to understand is that when current_node is NULL, the compiler doesn't see it as NULL and iterates one more time.

    void printBranches(struct bank *head) {
    struct branch *current_node = head->branches;
    while (current_node != NULL) {
        printf("%s ", current_node->bname);

        current_node = current_node->nextb;
    }
}

This is my print function.

Even though I use current_node!=NULL condition it prints something like this:' ╨q%+☺ ' when it is supposed to be NULL which I assume that it points somewhere unused in the memory(that has no meaningful value inside of it). Here my debugger shows addresses it points;

 current_node 0x27e6b5d1740 when it is not NULL

              0x27e6b5d17f0 when it is suppose to be NULL
              0xbaadf00dbaadf00d when compiler sees it as NULL

Any help would be appreciated. Thanks in advance.

Edit: I've realized that in the method I use for filling linked list's node after determining the name of the node I immediately create a new node in the memory and then go to next without setting it to NULL as @trincot mentioned in the comments.

  memcpy(new_branch->bname, entity, sizeof(entity));

        // Go next branch
        new_branch->nextb = malloc(sizeof(struct branch));
        new_branch = new_branch->nextb;

That's why the last non-filled node is not empty.

Edit 2: When I do what @trincot said in the comments I can not add a new node after the first node. So I've tried a different method.

while (current_node->nextb->nextb != NULL) {
    printf("%s ", current_node->bname);

    current_node = current_node->nextb;
}

when I do this problem is solved because I can print what I want without meaningless value.

10
  • 2
    It would help to see a minimal reproducible example that demonstrates the problem. Either current_node->nextb is not NULL when you think it should be, or current_node->bname does not point to a valid string. Creating an example might help you to find the issue yourself or allow others to help you diagnose the issue. This simple example stops when it should: ideone.com/HnFHGu Commented Apr 29, 2022 at 2:23
  • 1
    If it prints something like ' ╨q%+☺ , then current_node is clearly not NULL. On a desktop system, current_node->bname would cause a protection violation (SIGSEGV), killing the program. It would not be around to print garbage. Commented Apr 29, 2022 at 2:36
  • "0x27e6b5d17f0 when it is suppose to be NULL": please provide the code that sets it to NULL and then prints it like this. This cannot happen. Instead you did not set it to NULL. If you really think you did, provide the code that we can reproduce the issue with. Commented Apr 29, 2022 at 6:27
  • 1
    If you didn't set it to NULL, then that is the problem. You should never leave pointers uninitialised. Commented Apr 29, 2022 at 13:31
  • 1
    The clear implication is that head->branches is not NULL for the first element... therefore it will print something Commented Apr 29, 2022 at 14:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.