Cannot compare TSystem::HostName()

Hi All,

I am doing some stupid mistake here but I cannot figure it out:

in my root code I do:

std::cout << gSystem->HostName() << std::endl;
if (gSystem->HostName() == “”)
std::cout << “test” << std::endl;

on runtime it says:

and thats it. How come it does the == wrong? I also tried
strcmp(gSystem->HostName(), “”)
also does not go into the if …

How can I compare the result of the hostname method to a string??



have a look at the definition of the return code of strcmp - it returns 0 for equality. You could also use TString hostname(gSystem->GetHostname()), and then compare the TString using its operator==. The way you invoke the operator== C++ compares the memory addresses of the const char* returned by GetHostName and your local const char* “” - which will obviously always fail. So const char* hostname=gSystem->HostName(); if (hostname && !strcmp(hostname, ""))will work for you.


thanks it works now!