Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Returning a string from a function C++

MkunkMkunk Posts: 4Member
Hello,

I would like assistance on creating a code that reads an integer and returns a string.

the only part that does not work is the function char* files.
This code it meant to construct a bunch of .cxx files with different input files to analyze. I have simplified the code to the error.

Thanks in advance.

[code]#include
#include
#include

char* files(int j);

using namespace std;

int main(){


//-------------------making strings for file names----------------------------------------------

string nam1("gammaP_PPipPim_TOF_Mpcut");
string nam2(".cxx");
string nam3("Ntuple_gammaP_PipPim_TOF_MPcut_");
string nam4(".root");


for (int i = 1; i <= 2; i++){
char c[10];
sprintf(c, "%d",i);
string create = nam3 + c +nam4; //For creation of Tfile root file
string name = nam1 + c + nam2;
ofstream out(name.c_str());

//------------------reading the files---------------------------------------

files(i);
out.close();
}
return 0;
}


char* files(int j){
if(j ==1){
string input1, input2, input3, input4;
input1 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43516*_*.root")"");
input2 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4352*_*.root")"");
input3 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4353*_*.root")"");
input4 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/*.root")"");
return input1, input2, input3, input4;
}
if(j ==2){
string input1;
input1 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4354*_*.root")"");
return input1;
}
}[/code]

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Oh!.. My!..
    Does "return a1, a2, a3, a4;" really compiles? Which compiler is it? (So, I will not recommend it... ;-) ).

    On a more serious note: returning a "char*" means two things.

    1. You provide a buffer into a function, the function will fill it and then same function returns same buffer, like strcpy().

    2. You allocate a string memory inside a function, fill it with data and then return it. In this case you are responsible for freeing that memory somewhere later in code, when returned pointer is no longer needed.

    In your case you are dealing with a 'string' object. It is a little more than just a pointer. You need to reserve these strings somewhere BEFORE the function call and then pass into a function the reference to the objects. And since you need a few values to return - I suggest an array of 'string' objects:[/color]
    [code]
    void GetData (string a [])
    {
    // input into a[0], a[1], a[2], a[3]
    }
    ...
    string MyStrings [4];
    GetData (MyStrings);
    ...
    [/code]
  • MkunkMkunk Posts: 4Member
    Thank you for the advice.
    I simply put return input1, input2, input3..etc to show the fact I was trying to return more then one string at a time. I see how I should have worded that better.

    After reading your advice I noticed that it will not accomplish what I want it to, I would like to create 54 .cxx files therefore I must check inside my function an integer.
    similar example from before:
    [code]For(int i = 1; i <=53; i++){
    ...
    ...
    ...
    GetData(i);
    ...
    ...
    }
    [/code]

    Now in the function Getdata, I must check to see value is being passed and return the appropriate amount of strings.
    For every integer of "i", the number of strings that must be passed from GetData varies from 1-7 strings.

    Also I was unclear about your last idea. If you would please expound some more with an example code.

    Thanks very much
  • Malcolm_McLeanMalcolm_McLean Posts: 253Member ✭✭
    return x, y, z;

    is the comma operator. I don't think it has a use, aside from maybe some macro hackery, but it is part of the C++ standard and compilers are obliged to support it.

    (A comma operator, not to be confused with the use of commas as separators, is effectively the operator equivalent of a no-op. It evaluates both operands and returns the right-hand one.)
  • MkunkMkunk Posts: 4Member
    : return x, y, z;
    :
    : is the comma operator. I don't think it has a use, aside from maybe
    : some macro hackery, but it is part of the C++ standard and compilers
    : are obliged to support it.
    :
    : (A comma operator, not to be confused with the use of commas as
    : separators, is effectively the operator equivalent of a no-op. It
    : evaluates both operands and returns the right-hand one.)
    :


    My statement before this reply was made explains why I wrote that.
  • MkunkMkunk Posts: 4Member
    I have figured it out.
    For anyone who wants to know how I did it, see the following code.

    [code]#include
    #include
    #include
    using namespace std;

    vector< vector > getName(int );

    int main()
    {

    //-------------------making strings for file names----------------------------------------------

    string nam1("gammaP_PPipPim_TOF_Mpcut");
    string nam2(".cxx");
    string nam3("Ntuple_gammaP_PipPim_TOF_MPcut_");
    string nam4(".root");

    for(int j = 1; j<=3; j++){
    char c[10];
    sprintf(c, "%d",i);
    string create = nam3 + c +nam4; //For creation of Tfile root file
    string name = nam1 + c + nam2;
    ofstream out(name.c_str());

    vector< vector<string> > name = getName(j);
    int size = name.size();

    vector name1 = name[0];

    for(int i=0; i > getName(int j)
    {
    if(j==1){
    string input1 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43516*_*.root")"");
    string input2 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4352*_*.root")"");
    string input3 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4353*_*.root")"");
    string input4 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/*.root")"");

    vector name1;

    name1.push_back(input1);
    name1.push_back(input2);
    name1.push_back(input3);
    name1.push_back(input4);

    vector< vector > name;
    name.push_back(name1);
    return name;
    }

    if(j==2){

    string input1 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_4354*_*.root")"");
    vector name1;

    name1.push_back(input1);

    vector< vector > name;
    name.push_back(name1);
    return name;
    }

    if(j==3){

    string input1 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43550_*.root")"");
    string input2 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43551_*.root")"");
    string input3 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43552_*.root")"");
    string input4 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43553_*.root")"");
    string input5 = (" out << " fReader->Add("/disks/msd01/work/chandra/g11_skim_new/run_43500/run_43554_*.root")"");

    vector name1;

    name1.push_back(input1);
    name1.push_back(input2);
    name1.push_back(input3);
    name1.push_back(input4);
    name1.push_back(input5);

    vector< vector > name;
    name.push_back(name1);
    return name;
    }
    } [/code]
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Thanks! Nice to know that, however, I still stand by my point. Compiler should not blindly implement comma operator everywhere, only in logical places for it. The [italic]return[/italic] statement is obviously the wrong place for it. Can you provide the example where it would be good to use in a [italic]return[/italic]?[/color]
  • LundinLundin Posts: 3,711Member
    : [color=Blue]Thanks! Nice to know that, however, I still stand by my
    : point. Compiler should not blindly implement comma operator
    : everywhere, only in logical places for it. The
    : [italic]return[/italic] statement is obviously the wrong place for
    : it. Can you provide the example where it would be good to use in a
    : [italic]return[/italic]?[/color]


    Recognized coding standard such as MISRA-C bans the comma operator entirely. It is just another of those superfluous dumb things in the C language that only leeds to unreadable, possibly dangerous code.

    For example, nobody can know what code like this will do:

    if(x == str[i], i++)

    The order of evaluation of the comma operator is undefined in the standard, and thus the above code can either work or lead to bad crashes.

    Don't use the comma operator at all. There is never a reason to.
  • homodiohomodio Posts: 6Member
    This post has been deleted.
  • bottley8bottley8 Posts: 5Member
    This post has been deleted.
Sign In or Register to comment.