简体   繁体   English

比较 C 中的 char* 和 char[]

[英]Compare a char* with char[] in C

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:我正在尝试比较 C 中的 char[100] (nodo->fname) 和 char* (name),并且在比较两者时我在 strcmp function 中遇到分段错误:

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) (已经 malloc() nodo 在代码的其他部分)

Is this the way I should compare them?这是我应该比较它们的方式吗? If not how should I?如果不是我该怎么办? Thank you!谢谢!

The problem was that I was not checking if name could be NULL, thanks guys!问题是我没有检查名称是否可能是 NULL,谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM