c++ - !strcmp as substitute for == -


i'm working rapidxml, have comparisons in code:

if ( searchnode->first_attribute("name")->value() == "foo" ) 

this gives following warning:

comparison string literal results in unspecified behaviour [-waddress] 

is idea substitute with:

if ( !strcmp(searchnode->first_attribute("name")->value() , "foo") ) 

which gives no warning?

the latter looks ugly me, there else?

you cannot in general use == compare strings in c, since compares address of first character not want.

you must use strcmp(), endorse style:

if( strcmp(searchnode->first_attribute("name")->value(), "foo") == 0) {  } 

rather using !, since operator boolean operator , strcmp()'s return value not boolean. realize it works , well-defined, consider ugly , confused.

of course can wrap it:

#include <stdbool.h>  static bool first_attrib_name_is(const node *node, const char *string) {     return strcmp(node->first_attribute("name")->value(), string) == 0; } 

then code becomes more palatable:

if( first_attrib_name_is(searchnode, "foo") ) {  } 

note: use bool return type, standard c99.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -