3

I have a function where I would like to return multiple arrays. I know that in c++ does not return an array, but instead returns a pointer to an array. For example:

int* function(double array[])

But what if I need to return multiple arrays (multiple pointers to arrays? say like 2-10). I have thought of one way of doing this. One would be to just pass the arrays to a void function by reference:

void function(int a[], int b[], double c[])

But then we might be passing a lot of arrays as input. I could package all my input arrays into a class, pass the class by reference, but this seems like unnecessary structure. What is the right way to do this? Thanks

12
  • But what if I need to return multiple arrays My first reaction would be "find another way", although if it really is warranted then wrap the arrays in a struct. Either way you should look into std::array which can be returned by value.
    – user657267
    Sep 12, 2014 at 0:42
  • 1
    @nneonneo I am fairly new to c++ and was not aware that arrays were frowned on.
    – James
    Sep 12, 2014 at 0:44
  • 1
    'What is the right way to do this?' there is no right way, that's the beauty of programming! It all depends on the application. Btw, the solution you wrote is dangerous because 'function' doesn't know how much memory you're allocated for your arrays.
    – chuck1
    Sep 12, 2014 at 0:48
  • 1
    If you want to “return” three arrays, two of ints and one of doubles like your code suggests, I don't see a way to do this in a type safe manner without introducing a data type that has three members of the correct type. Maybe std::tuple could help you?
    – 5gon12eder
    Sep 12, 2014 at 0:50
  • 1
    @user657267 "pass by reference" (in computer science jargon) means that the callee accesses the same variable as the caller; in C++ passing a pointer is a way of achieving this, because the callee can dereference the pointer to access the variable in question. I think OP meant that, rather than the C++ term "reference".
    – M.M
    Sep 12, 2014 at 1:09

3 Answers 3

4

You can use std::vectors instead of arrays:

void function(vector<int> &a, vector<int> &b, vector<double> &c)

The function caller simply makes three vectors and passes them in:

vector<int> a, b;
vector<double> c;
function(a, b, c);

and the function can edit the vectors any way it likes (e.g. a.resize(10); a[0] = ...), since the vectors that the function uses are the same vectors that the caller passed in. In this way, the function can "return" multiple vectors by changing the vectors passed in by the caller.

0

You can use a vector of vectors

 std::vector<std::vector<double>> my2Dvec;
 std::vector<double> vec1, vec2, vec3;

 my2dvec.push_back(vec1);
 my2dvec.push_back(vec2);
 my2dvec.push_back(vec3);

 my2dvec[0].push_back(123.123);
 my2dvec[1].push_back(456.456);
 my2dvec[2].push_back(789.789);

std::cout << my2dvec[0][0] << std::endl; // 123.123
std::cout << my2dvec[1][0] << std::endl; // 456.456
std::cout << my2dvec[2][0] << std::endl; // 789.789
 //----------------------------------------

NOTE: vectors become increaseingly slow the more demensions you put into them. So if you have a 3d vector of vectors be mindful of the speed hit

1
  • Took it out. Hmmm I thought I did that recently. I'll test it later.
    – Jay
    Sep 12, 2014 at 0:56
0

Try using the following:

void Function(unsigned char*&dat1, unsigned char*&dat2, unsigned char*&dat3, ...)
{
      dat1 = new unsigned char[10];
      dat2 = new unsigned char[20];
      dat3 = new unsigned char[30];
      ....
}

This will pass the pointer of the array by reference.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.