C Strings Comparison with Equal Sign

后端 未结 5 623
借酒劲吻你
借酒劲吻你 2020-11-29 12:45

I have this code:

char *name = \"George\"

if(name == \"George\")
   printf(\"It\'s George\")

I thought that c strings could not be compare

5条回答
  •  感情败类
    2020-11-29 12:58

    If you compare two stings that you are comparing base addresses of those strings not actual characters in those strings. for comparing strings use strcmp() and strcasecmp() library functions or write program like this. below is not a full code just logic required for string comparison.

    void mystrcmp(const char *source,char *dest)
    {
        for(i=0;source[i] != '\0';i++)
            dest[i] = source[i];
       dest[i] = 0;
    
    }
    

提交回复
热议问题