C Strings Comparison with Equal Sign

后端 未结 5 607
借酒劲吻你
借酒劲吻你 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:59

    This will fail, since you are comparing two different pointers of two separate strings. If this code still works, then this is a result of a heavy optimization of GCC, that keeps only one copy for size optimization.

    Use strcmp(). Link.

提交回复
热议问题