0

I am trying to compare a char[100] (nodo->fname) with char* (name) in C and i am geting a segmentation fault in the strcmp function when comparing both:

struct mmap_info{
   char fname[100];
}

pos search_mmap(list *l, char *name){
    for(pos p = first(*l); !end(*l, p); p = next(*l, p)){
        struct mmap_info *nodo = get(*l, p);
        if(strcmp(nodo->fname, name) == 0){
            return p;
        }
    }
    return NULL;
}

(Already malloc() nodo in other part of the code)

Is this the way I should compare them? If not how should I? Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
rascadux
  • 56
  • 6
  • 1
    what is `list`, what is `pos`? – tstanisl Nov 17 '21 at 18:07
  • 3
    Lots of stuff missing here. Step through it with a debugger. Add some checks for `NULL`. Create a [mcve]. – 001 Nov 17 '21 at 18:07
  • `nodo->fname` will automatically be transformed to `char*`. The comparison should be fine – tstanisl Nov 17 '21 at 18:08
  • So the problem must be that either `nodo` or `name` is invalid. – Barmar Nov 17 '21 at 18:08
  • 1
    If you are getting a segmentation fault in `strcmp`, then the most likely reason is that at least one of the two function arguments does not point to a valid null-terminated string. Since you are not showing us what they are pointing to, we cannot tell you what is wrong. I suggest that you inspect the contents of the memory that these pointers are pointing to, using a [debugger](https://stackoverflow.com/q/25385173/12149471). If the problem persists, please provide a [mre] of the problem, or, if you are unable to, then please at least provide extra debugging information. – Andreas Wenzel Nov 17 '21 at 18:11
  • The problem was that I was not checking if name could be NULL, thanks guys! – rascadux Nov 17 '21 at 18:27
  • @DanielRodriguez: There was nothing wrong with posting an answer to your own question. See this official help page for further information: [Can I answer my own question?](http://stackoverflow.com/help/self-answer) – Andreas Wenzel Nov 17 '21 at 18:29

1 Answers1

0

The problem was that I was not checking if name could be NULL, thanks guys!

rascadux
  • 56
  • 6