Code:

char *CPFC = TEXT("C:\\Users\\Vasja-PC\\source\\repos\\Test\\Release\\Test.exe"); char CFP[128]; GetModuleFileName(NULL, CFP, 128); if (CFP == CPFC) printf("Good!"); 

With this comparison, the message Good! I do not receive, although when I try to compare 2 lines directly into if this message is displayed. This problem is not exactly related to the inequality of the lines, since I derived both lines and they were completely identical.

  • First, null-terminated strings in C and C ++ are not compared and never compared by the == operator. if nothing to do with it. Secondly, if the type of the string is hard-coded, the array char , what does the macro TEXT do here? For what purpose did you use this TEXT ? - AnT
  • How then can they be compared? - Aleksei .C
  • 2
    strcmp has always been. - AnT
  • Thank! Everything is working now. - Aleksei .C

1 answer 1

C ++ string comparison:

 if (strcmp(CFP, CFPC)) 

Warning : The result of the function strcmp() 0 (wrong) when the strings are identical.

  • 3
    The result of the strcmp function is not “true / false”, but a multi-valued integer value. Therefore, using strcmp (including the popular ugly antipattern !strcmp ) without explicit comparison is a bad practice. - AnT
  • @AnT - I wrote 0 - not true only in brackets. - MarianD
  • @MarianD, strcmp does not return a boolean value (0 or 1), but -1, 0, +1, depending on whether the left string is smaller than the right one, is equal to or greater than (smaller-greater than - in the sense of lexicographical order ). “Wrong” has nothing to do with it. - insolor
  • @insolor - See my previous comment. In the context of my answer, the result is in particular правда or неправда . - MarianD
  • @MarianD, what is the former, what is the present - is a collection of low-related words. strcmp does not return the truth is not true, even in brackets, even without. - insolor