Understanding Char Array equality in C

后端 未结 5 869
忘了有多久
忘了有多久 2021-01-19 13:47

Sorry in advance for the ignorance. I don\'t fully understand how to compare char arrays in C. I was originally comparing two char arrays in c with the simple ==

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 14:36

    Doing this if (a == b) will compare Pointer values stored in a and b.

    So if you have a

      a //say at some random address 1000 
    

    or b

      b //say at some random address 2000 
    

    Is a==b? Now normally if the compiler is doing string pooling and if your string literals are exactly same then this may work in those cases - otherwise you have to do a character-by-character comparison as strcmp would i guess.

提交回复
热议问题