0

I've been noticing this strange behaviour with string concatenation despite the fact that strcmp returns zero, showing both forms are identical

The include file options.h is as follows

struct options = {
   std::string ctifile;
   ...
};

The main file is being written in two ways

Method 1

#include "options.h"
#include <string>
#include "cantera/IdealGasMix.h"

options opt = {
  "mech/tot.cti"
};

IdealGasMix gas(opt.ctifile, ...);

In the second method,

#include "options.h"
#include <string>
#include "cantera/IdealGasMix.h"

options opt = {
  "tot.cti"
};

IdealGasMix gas(std::string("mech/") + opt.ctifile, ...);

For some reason, only Method 2 is unable to find the file specified. Any pointers? (pun intended)

gpavanb
  • 288
  • 2
  • 12
  • 1
    [No reason it shouldn't work.](http://cpp.sh/3yln) – amanuel2 Oct 08 '16 at 18:38
  • Something else is going wrong, and it isn't clear from what you posted. You need to come up with a [mcve] that shows the issue. – MicroVirus Oct 08 '16 at 18:40
  • Working for me. I suspect on you gas function argument declaration – Pavan Chandaka Oct 08 '16 at 18:45
  • Don't summarize code when you don't understand what's wrong with it. Remove some of the code and recompile; if it still fails, repeat. When it doesn't fail, either you'll recognize what's wrong, or you can back up to the last one that failed and analyze it further. – Pete Becker Oct 08 '16 at 18:49

1 Answers1

1

strcmp expects cstrings which you can get by going

std::string x = "blah blah";
assert(strcmp(x.c_str(), x.c_str()) == 0);

but if you're using std::strings anyways why not just use std::string::compare

std::string x = "blah blah";
assert(x.compare(x) == 0);

http://www.cplusplus.com/reference/string/string/compare/

caveat: if you're trying to do compare unicode strings then simple strcmp might not always work

James Deng
  • 173
  • 10