String to const char with sprintf does not work

Hi,

can someone tell me why the following script does not work and how do I make it work.

{
string histoArray[2]= {
  "L2_Cosmic_Muon_DeltaPt_vs_eta_DT_top",
"L2_Cosmic_Muon_DeltaPt_vs_eta_DT_bottom"
};
char histoname[100];
int n;
for(int i=0;i<2;i++) {
  cout << histoArray[i] << endl;
  n = sprintf(histoname,"%s",histoArray[i]);
  cout << n << endl;
  cout << histoname << endl; // I get weird stuff here
}
}

here the output I get:

root [1] .x test.C
L2_Cosmic_Muon_DeltaPt_vs_eta_DT_top
8
$\uffff
D\uffff

L2_Cosmic_Muon_DeltaPt_vs_eta_DT_bottom
4
D\uffff

root [2]

Hi,

You need n = sprintf(histoname,"%s",histoArray[i].c_str());

Cheers,
Philippe.

Thank you, that does work! I do not think I will ever understand strings:(

By the way, can something similar can be done with TString. I mean can one convert TString to const char*? In other words, how do you use operator const char* (). Just doing TString histoArray[2]= { "L2_Cosmic_Muon_DeltaPt_vs_eta_DT_top", "L2_Cosmic_Muon_DeltaPt_vs_eta_DT_bottom" }; char histoname[100]; for(int i=0;i<2;i++) { const char* histoArray[i](histoname); }

Did not work.

Dmitry.

Hi,

Use myTString.Data()

Cheers,
Philippe.