Function that returns an unknown number of strings

Hi all, here is the issue I'm facing. I create a function to which I enter an integer and a string. It should then return a number of strings which number is unknown.So since the number is not known up front I use vector.

This is what I have and it gives me "address of local variable 'aux' returned":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string *my_function(int akr, string chor)
{
      vector<string> weeks;
      // ...
      // code here where I calculate the vector weeks: a vector of strings
      // ...
      
      // Then, once I have weeks I use an auxiliary aux to convert to string as below:

      string aux[weeks.size()];

      for(int j=0; j<weeks.size(); j++)
      {
	       	aux[j] = weeks[j];
      }

      return aux;
}

int main(int argc, char *argv[])
{  
      //here I print the second string of my_function() with the selected
      //parameters. It actually prints it but I still get the warning.
      cout<<my_function(2,"FgS7DFd")[1]<<endl;
}


I understand that aux is local so it's being removed from the memory once the function run is over but

1.how do I get around that?

2.Also, notice how I used the auxiliary aux to convert the vector to a string because if I did return weeks; I was getting a compilation error. Any way I can avoid that?

Thanks!
Last edited on
well your not using (int akr, string chor) in the function so whatever your sending it isn't being used...

you can send the address at variable, if you need to change / return more than 1 var.

eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string my_function(int& something, string aux)
{
      vector<string> weeks;
      // ...
      // code here where I calculate the vector weeks: a vector of strings
      // ...
      
      // Then, once I have weeks I use an auxiliary aux to convert to string as below:

      string aux[weeks.size()];

      for(int j=0; j<weeks.size(); j++)
      {
	       	aux[j] = weeks[j];
      }

      return aux;
}

int main(int argc, char *argv[])
{  
      //here I print the second string of my_function() with the selected
      //parameters. It actually prints it but I still get the warning.
      cout<<my_function(2,"FgS7DFd")[1]<<endl;
}
Last edited on
thanks.

But no, I actually use (int akr, string chor). I just didn't include it as it's not where I'm focusing. I use (int akr, string chor), and then come to the point where I have calculated the vector 'weeks'.

Then what?
Take this for example: I will return a double, and manipulate a string and integer.
// the "address of" operator is the '&'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
double function(double d1, string& str, int& x)
{
   // manipulate string:
   for (int i=0; i<str.size(); i++)
      str[i] = toupper(str[i];

   // manipulate x
   x += 100;

   // manipulate d1 and return
   return ( d1 + 36.2 );
}

int main()
{
   int x=1; // these can be named whatever...
   double turkey=1.0;
   string poopie="I like Pie!!!";

   turkey = function ( turkey, poopie, x );
   cout << turkey << '\n';
   cout << poopie << '\n';
   cout << x << endl;

   return 0;
}



37.2
I LIKE PIE!!!
101

edit:
ok how about this instead:
1
2
3
4
5
6
7
8
9
10
11
12
void function (string & str )
{
   str += " Hello Kitty!";
}

int main()
{
   string aux="";
   function(aux);
   cout << aux;
   return 0;
} 
Last edited on
yes sure but the difference is that (which is in fact what's causing the problem) the function I need it to return

an unknown number of strings

and this is what I can't get around.

your want to return a vector of strings?
how about passing in a vector of 0 strings, the function can manipulate it...
or
1
2
3
4
5
6
vector<string> function(...)
{
   ...

   return vector<string> name;
}


does aux have to be an array?
AFAIK the assignment operator works just fine on arrays, so you can pass in an array of strings, create a new array of strings in the function assign it to the passed in "array &"
Last edited on
OK. Problem solved as per your suggestion:

1
2
3
4
5
6
7
8
vector<string> function(...)
{
   ...
   vector<string> name;
   // do whatever you do
  
   return name;
}


Now I have another question that naturally arises out of this. Here is my main()
1
2
3
4
int main(int argc, char *argv[])
{  
      cout<<function(2,"FgS7DFd")[1]<<endl;
}


This will print the second string in the function's return.
HOW DO I GET TO PRINT ALL OF THEM?

I would run a for(i=0; i<SIZE OF THE RETURN, i++) but how do I access the size of the return vector?

Topic archived. No new replies allowed.