0

I have a method called getNode which takes a char str[] looks through a Linked List (struct Node* ptr) After adding all elements to my linked list, I'm then saying:

This is what's inside my main Method

struct Node* X
X = getNode(str, temp)` 

Here is my getNode method

struct Node* getNode(char str[], struct Node* start) {
    struct Node* ptr = start;
    while (ptr != NULL) {
    //  printf("Comparing [%s] with %s\n", str, ptr->varName);
        if (!strcmp(ptr->varName, str)) {
            return ptr;
        }
        ptr = ptr->next;
    }
    //printf("going to return null");
    return NULL;
}

then I make a condition back inside my main method

if (X == NULL){
// will do something
}

But I'm getting a segmentation fault between these two codes, how do I fix this?

econCodergirl
  • 315
  • 1
  • 3
  • 8
  • 4
    This type of questions are unanswerable without [mcve] provided. – Eugene Sh. Dec 09 '19 at 18:30
  • 1
    Please provide the definition of `getNode` and any and all code related to how you call it and the arguments you are passing to it. Without that it could be literally anything. Please also check out answers like [this one](https://stackoverflow.com/a/2876374/982257) on how to use `gdb` to find out where your segfault is occurring, as well as [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). But if you can add more details I'll vote to reopen your question. You weren't given much time to do it! – Iguananaut Dec 09 '19 at 18:35
  • Thanks, I added more details. Let me know if I should add anything else – econCodergirl Dec 09 '19 at 18:59
  • 1
    still not enough. As already said, you need to provide an MRE. The seg fault is surely the result of some undefined behavior, and nothing you've posted here shows that. The problem is in code elsewhere that you haven't shown. My best guess is `varName` is not getting initialized correctly. – yano Dec 09 '19 at 19:12
  • You wrote "This is inside my main Method: ..." except it isn't because it isn't even quite valid C code. Please just copy and paste *exactly* what you have. E.g. how was the variable `temp` initialized? What about `str`? – Iguananaut Dec 10 '19 at 10:42

0 Answers0