Warning: comparison with string literals results in unspecified behaviour
You want to use strcmp() == 0
to compare strings instead of a simple ==
, which will just compare if the pointers are the same (which they won't be in this case).
args[i]
is a pointer to a string (a pointer to an array of chars null terminated), as is "&"
or "<"
.
The expression argc[i] == "&"
checks if the two pointers are the same (point to the same memory location).
The expression strcmp( argc[i], "&") == 0
will check if the contents of the two strings are the same.
comparison between string literal
char arrays or char pointers aren't really the same thing as string class objects in C++, so this
if (option == "foo")
Doesn't compare the string option
to the string literal "foo" it compares the address of option
with the address of the string literal "foo". You need to use one of the many string comparison functions if you want to know if the option is the same as "foo". strcmp
is the obvious way to do this, or you can use std::string
instead of char*
Comparison with string literal results in unspecified behaviour?
In C++ ==
only implemented internally for primitive types and array is not a primitive type, so comparing char[100]
and string literal will only compare them as 2 char*
or better to say as 2 pointers and since this 2 pointers can't be equal then items[n] == "ae"
can never be true, instead of this you should either use std::string
to hold string as:
std::string items[100];
// initialize items
if( items[n] == "ae" ) ...
or you should use strcmp
to compare strings, but remeber strcmp
return 0 for equal strings, so your code will be as:
char items[100][100];
// initialize items
if( strcmp(items[n], "ae") == 0 ) ...
And one extra note is if (items == 0)
is useless, since items
allocated on stack and not in the heap!
C++: warning: comparison with string literal results in unspecified behaviour [-Waddress]
Your comparison user->MyClass->name.c_str() == "user"
compares two pointers, and two pointers that will never be the same.
Since you seem to be using std::string
you can use the equality comparison operator directly without needing to get a pointer:
if (user->MyClass->name == "user") { ... }
If for some strange reason you want to compare using C-style string pointers, you must use std::strcmp
.
C++ Comparison of String Literals
You are comparing memory addresses. Apparently your compiler places the string literals in memory in the order it encounters them, so the first is "lesser" than the second.
Since in the first snippet it sees "A" first and "Z" second, "A" is lesser. Since it sees "Z" first in the second, "Z" is lesser. In the last snippet, it already has literals "A" and "Z" placed when the second command rolls around.
What's wrong with the conditions in this IF-ELSE?
userGuess == "h"
(and userGuess == "e"
)
You compare string literals using operator ==
which is not a valid string comparison in C - you should use standard library function for string comparison strcmp
instead1.
Otherwise, make userGuess
as single char
variable instead of array of char, and use single character (single quote) for comparison, such as
userGuess == 'h'
1
As it stands, the comparison userGuess == "h"
compiles but should give you an warning if you turn on gcc -Wall
compiler flag, for example:
warning: comparison with string literal results in unspecified behavior [-Waddress]
It means (as mentioned by Jonathan Leffler's comment below), you are not comparing the strings itself but instead comparing the two pointers (userGuess
and "l"
are pointing to the same location) - which is valid but generally meaningless.
Pylint complains about comparing a string to a literal with 'is'
is
checks that the left hand argument holds the exact same reference as the right hand argument. This is fine for None
which is a singleton, but is usually a bad idea for other types, where multiple instances can have the same logical value.
Consider, e.g. the following example:
>>> my_string = ''.join([c for c in 'xfje'])
>>> print my_string
xfje
>>> print my_string == 'xfje'
True
>>> print my_string is 'xfje'
False
Related Topics
Consistent Pseudo-Random Numbers Across Platforms
What Encoding Does Std::String.C_Str() Use
How to Do Input Validation in C++ with Cin
Does (W)Ifstream Support Different Encodings
Warning: Returning Reference to Temporary
Why Does Reallocating a Vector Copy Instead of Moving the Elements
Differencebetween Using a Struct with Two Fields and a Pair
Boost::Spirit Expression Parser
About Thread-Safety of Weak_Ptr
How to Query a Running Process for Its Parameters List? (Windows, C++)
How to Check If Window Is "Always on Top"
What's the Difference Between a Const Member Function and a Non-Const Member Function
Comparison Between String Literal
Cpack: Exclude Install Commands from Subdirectory (Googletest Directory)
"No Appropriate Default Constructor Available"--Why Is the Default Constructor Even Called
Order of Evaluation of Function Parameters
Calculate Mean and Standard Deviation from a Vector of Samples in C++ Using Boost