Function with return array

Hello,
I'm trying create a function that return a array with all ids.
I tried the below code but not worked

#include <iostream>
#include <string>

using namespace std;


string ide()
{
int nanim;
string id[nanim];
cout<<"Enter the number of animals: ";
cin>> nanim;
for (int i=0; i<nanim;i++)
{
cout<< "\nEnter id anim "<<i+1 << "\n";
cin>> id[i];

}
return id;
}

int main()
{

string id = ide();
cout << id <<"\n";
return 0;

}
This code of course is invalid by several reasons. First of all the return type of function ide is std::string while in the return statement there is array name id that is converted to the pointer to the first element of the array.
Also you should specify a constant expression as the size of an array. In the program variable nanim was not initialized so the next statement has undefined behavior.

1
2
int nanim;
 string id[nanim];


In such cases it is better to use std::vector instead of arrays.
Last edited on
How can do I rewrite this code for return array? I cant specify a constant expression as the size of an array because I do Know the size...
Thank's
I would write the function the following way (without testing)

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
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 

vector<string> ide()
{
   vector<string> id;
   vector<string>::size_type nanim;

   cout << "Enter the number of animals: ";
   cin >> nanim;
 
   id.reserve( nanim );

   for (  vector<string>::size_type i=0; i < nanim; i++ ) 
   {
      cout<< "\nEnter id anim "<< i+1 << ": "; 

      string s; 
      cin >> s;
      id.push_back( s );
   } 

   return id; 
}
 
int main()
{
   vector<string> id = ide();
   
   for ( const string &s : id )  cout << s <<"\n";

   return 0;
}
The other way if you are to use arrays is to allocate dynamically memory.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string * ide()
{
   size_t nanim;

   cout << "Enter the number of animals: ";
   cin >> nanim;
 
   string *id = new string[nanim];

   for (  size_t i=0; i < nanim; i++ ) 
   {
      cout<< "\nEnter id anim "<< i+1 << ": "; 

      cin >> id[i];
   } 

   return id; 
}
When I do the last code; I cant print all ids, i can print just one id...How can I do to print the array?
I tried:

#include <iostream>
#include <string>
using namespace std;

string * ide(size_t nanim)
{

cout << "Enter the number of animals: ";
cin >> nanim;

string *id = new string[nanim];

for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i];


}
return id;
}


int main()
{
size_t n;
string *id =ide(n);
int i=0;
cout << id[i] <<i + 1<< endl;
}
#include <iostream>
#include <string>
using namespace std;

string * ide(size_t &nanim)
{

cout << "Enter the number of animals: ";
cin >> nanim;

string *id = new string[nanim];

for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i];


}
return id;
}


int main()
{
size_t n;
string *id =ide(n);

for ( size_t i = 0; i < n; i++ ) cout << id[i] <<i + 1<< endl;
}
ok Thank you!
But I did not want for in the int main..
I tried
#include <iostream>
#include <string>
using namespace std;

string * ide()
{
size_t nanim;
cout << "Enter the number of animals: ";
cin >> nanim;

string *id = new string[nanim];
string *id2 = new string[nanim];

for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i];
cout << id[i] <<i + 1<< endl;

}
return id;
}


int main()
{
string *id =ide();
cout << id << "\n";
return 0;
}

But my output was:

Enter the number of animals: 2

Enter id anim 1: 22
221

Enter id anim 2: 333
3332
0xb25ed8

--------------------------------
Process exited with return value 0
Press any key to continue . . .

What is the 0xb25ed8?...

I woud like my id array :22
333

Thank you
Last edited on
0xb25ed8 is the address of the first element of the array that you are returning from the function.
Also it is not clear why you allocated two arrays.

string *id = new string[nanim];
string *id2 = new string[nanim];
I'm sorry
I put the wrong code

string * ide()
{
size_t nanim;
cout << "Enter the number of animals: ";
cin >> nanim;

string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";

cin >> id[i];
cout <<i + 1<< ":"<< id[i] << endl;

}
return id;
}


int main()
{
string *id =ide();
cout << id << "\n";
return 0;
}
It is not very important for the question that you put a wrong code.
In any case id is a pointer. So line

cout << id << "\n";

displays its value that is the address of the first element of the allocated array.

Topic archived. No new replies allowed.