0

I tried the code below to return an array with all string ids, but it didn't work. The output just returns a number. How can I return an array with ids?

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

string* getArray()
{   
    int 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];
    }
    for ( size_t i = 0; i < nanim; i++ ) 
    {
    cout << id[i] << endl; 
    }
return id;
 }

  int main()
{
 int n;
 cin>>n;
    string* anim[n]=getArray();
cout<<anim;
return 0;
}
8
  • 3
    You're not returning an array, you're returning a pointer to the first element of an array. You'd be much better off using std::vector. Commented May 22, 2013 at 21:17
  • You cannot return arrays from functions (or pass arrays to functions). That is all. Commented May 22, 2013 at 21:21
  • 1
    Of course you can, you just can't know the length. Commented May 22, 2013 at 21:22
  • 1
    @Joel - no, you are passing (or returning) a pointer to the array. While arrays will decay into pointers, they are not pointers. Commented May 22, 2013 at 21:24
  • yes, but at the level of this question you can return an array from a function. Saying it cant be done is just pedantic, not helpful Commented May 22, 2013 at 21:27

2 Answers 2

4

You are returning a pointer to the first element in the array.

To access array elements just having called string* arr = getArray(); you can use arr[0], arr[1], arr[2] etc. to access the strings.

Don't forget to delete the memory you allocated in the function though; at the moment you have a big memory leak.

Generally this is not good programming though since the function caller doesn't know how many elements there are in the returned array. It would be better to get the number of animals in the caller and pass that into your function.

Better still, rebuild your code to use std::vector as I see you're already using stl. Then you don't need to worry (explicitly) about memory allocation and deallocation.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Also, I suggest passing n to getArray() as a parameter and doing some sanity check on it.
Good point AlexK +1. Personally I also dislike code that allocates memory in a function and it's the caller's job to free it.
3

You do not need to read the number of elements twice, and the type of the anim should be string*, not string* []. Unfortunately, this wouldn't tell you the number of items in the array, so you need to get it from the getArray, for example, like this:

string* getArray(int& nanim) {
    // Remove the declaration of nanim, and keep the rest of the code unchanged
    ...
}

int main()
{
    int n;
    string* anim = getArray(n);
    for (int i=0; i != n; i++) {
        cout << anim[i] << endl;
    }
    delete[] anim;
    return 0;
}

This is not an optimal C++ solution, though: you would be much better off using std::vector instead of an array, because the vector grows dynamically, and its size is returned along with the container itself. There would be no need to delete[] the result either, which would significantly simplify your code:

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

vector<string> getVector()
{   
    int nanim;
    cout << "Enter the number of animals: ";
    cin >> nanim;
    vector<string> res;
    for ( size_t i=0; i < nanim; i++ ) 
    {
        cout<< "\nEnter id anim "<< i+1 << ": ";
        string tmp;
        cin >> tmp;
        res.push_back(tmp);
    }
    return res;
}

int main()
{
    vector<string> anim = getVector();
    for ( size_t i = 0; i < anim.size(); i++ ) 
    {
        cout << anim[i] << endl; 
    }
    return 0;
}

2 Comments

How can I do that using a vector?
@user2386222 Take a look at the edit. Here is the same code running on ideone: link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.