Pointer char confusion

First of all my appologies: I am new and confused and don’t even know how the real subject of my question.

I am trying to create a TGraph. Because I want to finally create many with a for loop, I want to sprintf the name first. Maybe it is best to give you my code in order to explain what I want to do:

 char filename[m];
 char tgraphname[m];
 
 int degreeop = 980;
 sprintf(tgraphname, "fPolish%d", degreeop);
 
 TGraph *tgraphname = new TGraph();

I get a compiler message like: conflicting declaration ‘TGraph* tgraphname’
TGraph *tgraphname = new TGraph();
^
meanvonangle.cc:32:8: error: ‘tgraphname’ has a previous declaration as ‘char tgraphname [(((sizetype)(((ssizetype)m) + -1)) + 1)]’
char tgraphname[m];

Okay, I did not really expect it to work, but how can I solve this?

Thanks and cheers!

Hi,

just replace

TGraph *tgraphname = new TGraph();

by

TGraph *myTGraph = new TGraph();

as you really shouldn’t re-use a variable name.

Cheers,
Benedikt

I have not expressed clearly what I would like to do. It is hard for me.

I would like to create a number of TGraph’s inside a loop. I need to give it a new name with each run through the loop, but I do not know how to pass the name to “*name”. Obviously it does not work like this:

for (i=0; i<5; i++)
{
sprintf(name, "graph%d, i);
TGraph *name = new TGraph();
}

In this simplified version, it would create 5 TGraphs with the names graph0, graph1, graph2, graph3 and graph4. How do I achieve this?

Thanks so much!!

{ for (Int_t i = 0; i < 5; i++) { TGraph *g = new TGraph(); g->SetName(TString::Format("graph%d", i)); // https://root-forum.cern.ch/t/trouble-with-tgraph-setname/19125/1 gROOT->GetListOfSpecials()->Add(g); // "hide" it in "Specials" } }

Super! Thank you!

In the meantime I have tried working it out with a TGraph array:

TGraph **graph = new TGraph*[5]; for(i=0;i<5;i++) { graph[i] = new TGraph; }