0

![enter image description here enter image description here enter image description here I'm baffled. I'm working on a project that requires the use of strcmp and it doesn't seem to be working at all. Above you see an example in gdb where two characters are said to be unequal. Obviously similar results when I compare my two char arrays. Further, when I compare the arrays with themselves I also get false (name above is a char[8]). When I run strcmp within my code on the same array it produces the correct results but not in gdb (seen in the third image) I just need a lead on where to look/what to do.

Thanks.

edit: the two code fragments from the images:

print strcmp("S", "S")==0
print strcmp(name, name)==0
A. Smith
  • 45
  • 1
  • 5
  • 4
    Are those single-quotes in your screenshot? (Side note: it's better to post the text as text, rather than as a graphic, specifically so the text can be easier to view and verify). If they are single-quotes, then it's likely that `strcmp()` isn't being called correctly -- it expects its arguments to be of type `const char *`, not `char`. So try it with double-quotes (i.e. `print strcmp("S", "S") == 0` instead. – Jeremy Friesner Dec 10 '19 at 05:02
  • 1
    C != C++. Please pick one – Mad Physicist Dec 10 '19 at 05:09
  • 1
    One function doesn't have enough arguments, the other has unknown arguments... – Mad Physicist Dec 10 '19 at 05:10
  • 2
    And you keep shifting the code around without posting it as text... – Mad Physicist Dec 10 '19 at 05:12
  • @MadPhysicist name is defined as a char[8] in my code that is currently running. If I, say, type "print name" then gdb prints out "lab7" – A. Smith Dec 10 '19 at 05:15
  • @A.Smith. Thanks for being responsive. Can you please create a minimal reproducible example? Can you please post it as text instead of images? – Mad Physicist Dec 10 '19 at 05:17
  • @MadPhysicist Would the behavior of strcmp in gdb be affected by my code though, especially if comparing the same string literals? I can't see how that could be the case. – A. Smith Dec 10 '19 at 05:20
  • 1
    @A.Smith I've just ran `print strcmp(name,name)==0` in `gdb` and got 1. So, unless we'll understand what differs between your and my setup, we can't help you. Please provide your source code and information about compiler and system. – Denis Sheremet Dec 10 '19 at 05:22
  • @MadPhysicist code src is up. – A. Smith Dec 10 '19 at 05:39
  • You really should trust the standard [strcmp(3)](http://man7.org/linux/man-pages/man3/strcmp.3.html), it behaves as documented. Bugs are much more likely in your code that in the standard libraries of C or of C++. But see [*How to debug small programs*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and read about [undefined behavior](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html). With [GCC](http://gcc.gnu.org/) compile with `gcc -Wall -g` your `*.c` C source code and with `g++ -Wall -g` your `*.cc` C++ source code (warnings + debug info) – Basile Starynkevitch Dec 10 '19 at 05:47
  • 1
    Possible duplicate of https://stackoverflow.com/questions/9610882/strcmp-in-gdb-giving-odd-results – akib khan Dec 10 '19 at 05:58
  • strcmp is meant to compare strings which end with \0 character. If your name buffer iddn't had space for it, it's possible that you have not equal string – Swift - Friday Pie Dec 10 '19 at 06:15
  • @Swift-FridayPie. I thought the same thing at first, but there's going to be a zero eventually. How can a buffer ever differ from itself? – Mad Physicist Dec 10 '19 at 12:09
  • @A.Smith. Please provide a minimal reproducible example – Mad Physicist Dec 10 '19 at 12:10
  • Did you by chance redefine `strcmp`? – dbush Dec 10 '19 at 12:20
  • @MadPhysicist if you mean gdb command, it doesn't. gdb doesn't call right address. It's an issue with library "functions" which are defined\implemented by C++ compiler. Strcmp and pow are most common examples of that fallacy. To call those one either should evaluate correct internal function name or add a wrapper function to program being debugged. – Swift - Friday Pie Dec 11 '19 at 10:11

2 Answers2

3

You're using the wrong set of quotes.

The strcmp function expects both arguments to be of type const char *, i.e. a pointer to a null terminated string. In C++ (and C) single quotes are used for single characters, while double quotes are used for null terminated strings. So you're not passing arguments of the proper type.

The proper way to do this comparison is:

strcmp("S", "S") == 0
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    @A.Smith You're calling a different function now, and with the wrong number of arguments. – dbush Dec 10 '19 at 05:09
  • Screenshots should be fixed now – A. Smith Dec 10 '19 at 05:17
  • this answer is correct, the issues is with your code not this function. `strcmp("S", "S") == 0` will return true. If you actually need help with this, post some of your code (why the screenshots?) – lenz Dec 10 '19 at 05:19
  • @calvinBroadus How could my code affect the result of "strcmp("S", "S") == 0" in gdb? In the same vein, what code would affect the result of "print (1==1)"? – A. Smith Dec 10 '19 at 05:22
  • 1
    You're saying that you're getting the wrong output, but it's impossible to reproduce. I just ran this exact function and got `true`... So unless you're sharing the relevant part of your code we can only assume that the error is with your code and not this function. – lenz Dec 10 '19 at 05:26
  • 1
    @calvinBroadus I just felt like it would be a headache for you to run this code, so I left it out. I put it up, though. – A. Smith Dec 10 '19 at 05:39
  • 1
    @A.Smith it is a headache. That's why there is this word "minimal" in [mcve]. You are supposed to remove the irrelevant parts from the code (but keep it running at all times) until just the problem remains – n. m. could be an AI Dec 10 '19 at 05:47
  • Ya that's not the most useful example you posted up there. – lenz Dec 10 '19 at 06:25
  • 1
    @calvinBroadus gdb evaluation of strcmp may return garbage while debugging C++ program, if strcmp was optimized out. Which results in `print strcmp(name, name)` returning random value, which might be 0 as well as it might not. So, OP mistaken his error at trying to evaluate function for indication of error in function's operation. – Swift - Friday Pie Dec 10 '19 at 06:44
  • Great, thanks for the insight. I was indeed just reading about compiler optimization in this case – lenz Dec 10 '19 at 06:52
2

When the strings are equal strcmp returns 0 , false , I think you are expecting 1 , true. But that's how it is. You can wrap it in your own function and your function can return true.

BobRun
  • 756
  • 5
  • 12
  • Often problems with strcmp are when you have a \n at the end of a string, watch for that, or a blank on start of the string. – BobRun Dec 10 '19 at 06:15