Home c++ How to pass an array to a function and return it?

How to pass an array to a function and return it?

Author

Date

Category

I know that almost all other languages ​​have a method type like int [] and I can return the array I passed.

Here is the code:

int A [3] = {1,2,3};
int Func (int Array []) {
for (i = 0; i & lt; 3; i ++) {
  Array [i] = + 1;
}
return Array;
}

How to do this?


Answer 1, authority 100%

The fact is that in C++ only a pointer (or reference) to an array can be passed to a function, so you will always work with the original array, not a copy of it (and you will also return a pointer to the original array):

int * Func (int * Array)
{
  for (i = 0; i & lt; 3; i ++) {
    Array [i] ++;
  }
  return Array;
}

Also, this code does not take into account the size of the array (the loop goes through exactly three times), which can lead to the following:

  • if the length of the input array is greater than 3: only the first three elements of the array will be processed;
  • if the length of the input array is less than 3: the program will crash with a crash and a memory access error.

The size problem can be solved in two ways: to pass the size of the array additionally to the function, or to pass the array by reference (if you pass the array by reference, errors with the fact that you have passed or returned an array of the wrong size will be detected at compile time):

int * Func (int * Array, int size) {...} // passing the size, then the loop will for (int i = 0; i & lt; size; i ++)
int (& amp; Func (int (& amp; Array) [3])) [3] {...} // pass by reference, then the loop will be for (int i = 0; i & lt; 3; i ++)

In both cases, a pointer (or reference) to the original array will be returned. But there is no point in returning anything from the function in this case, you can just continue to use the old variable.


To return a new array, you just have to allocate memory for a new array inside the function and return a pointer to it (the main thing is not to forget to clear the memory later):

int * Func (int * Array, int size)
{
  int * new_array = new int [size];
  for (int i = 0; i & lt; size; i ++) {
    new_array [i] = Array [i] + 1;
  }
  return new_array;
}


But we have C++, damn it, so instead of standard C-arrays, you should use the containers provided by Homeland STL:

  • std :: vector & lt; type & gt; such a resizable “array”
  • std :: array & lt; type, size & gt; fixed size array.

Now we can use iterators, Range-based for loop (since C++ 11) and other niceties:

std :: vector & lt; int & gt; Func (std :: vector & lt; int & gt; Array) // example with vector and a loop with a counter
{
  for (int i = 0; i & lt; Array.size (); i ++) {
    Array [i] ++;
  }
std :: array & lt; int, 3 & gt; Func (std :: array & lt; int, 3 & gt; Array) // with array and a cool loop with iterators
{
  for (std :: array & lt; int, 3 & gt; :: iterator it = Array.begin (); it & lt; Array.end (); it ++) {
    * i ++;
  }
  return Array;
}

Answer 2, authority 14%

Not a C++ expert, but I can say that you are not passing an array, but a function and a pointer to its first element. In this case, you are not working with a local copy of the array, but with its pointer.

If the task of the int Func (int Array []) function is to modify the “passed array ” you don’t have to return it, but if you really want to to return something to the place of the call (which in this case is pointless) – then you need to change the function signature:

int * Func (int Array []) {
  for (i = 0; i & lt; 3; i ++) {
    Array [i] + = 1; // this is probably the case. The = + operation is meaningless.
  }
  return Array;
}

Notice the return type of int * is a pointer to int .


Answer 3, authority 10%

A pointer to an array is passed and the work is carried out directly with it, therefore, it is not at all necessary to make a return, for example:

void Func (int * array, size_t arraySize) {
 for (size_t i = 0; i & lt; arraySize; i ++) {
  array [i] + = 1;
 }
}

Answer 4, authority 5%

As an example, the getSorted function takes an array source (int []) and its size.
Makes a copy, sorts, and returns a sorted array.

int * getSorted (int * source, int size)
{
  int * rc = (int *) malloc (sizeof (int) * size); // fixed
  int tmp = 0;
  for (int i = 0; i & lt; size; i ++)
    rc [i] = source [i];
  for (int i = 1; i & lt; size; i ++)
  {
    for (int r = 0; r & lt; (size - i); r ++)
    {
      if (rc [r] & gt; rc [r + 1])
      {
        tmp = rc [r];
        rc [r] = rc [r + 1];
        rc [r + 1] = tmp;
      }
    }
  }
return & amp; rc [0];
}

Call example:

int * target;
int source [10];
....
// filling the array source
source [0] = 12; ....
...
target = getSorted (source, 10);
// further we treat target as an array
int m = target [3];

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions